PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > including general variables in modules
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
including general variables in modules

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 16h49   #1
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut including general variables in modules

I'm trying to create different scenarios so I thought to do something
like:

file:scenarios.rb
------------------
module Scenarios
module Default
$bab="defaultmodulevalue"
end
module DefaultHidden
$bab="hiddenmodulevalue"
end
module DefaultSecond
$bab="secondmodulevalue"
end
end

file:mytest.rb
---------------
require 'scenarios.rb'
include Scenarios:efault
...
puts $bab

--------------

but it doesn't matter which module I include I always have the same
respond:
"secondmodulevalue"

Any ideas for resolving this?

Thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 17h12   #2
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

On Mar 12, 2008, at 11:49 AM, Mario Ruiz wrote:

> I'm trying to create different scenarios so I thought to do something
> like:
>
> file:scenarios.rb
> ------------------
> module Scenarios
> module Default
> $bab="defaultmodulevalue"
> end
> module DefaultHidden
> $bab="hiddenmodulevalue"
> end
> module DefaultSecond
> $bab="secondmodulevalue"
> end
> end
>
> file:mytest.rb
> ---------------
> require 'scenarios.rb'
> include Scenarios:efault
> ...
> puts $bab
>
> --------------
>
> but it doesn't matter which module I include I always have the same
> respond:
> "secondmodulevalue"
>
> Any ideas for resolving this?
>
> Thanks.



Uh, stop using global variables?

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com



  Réponse avec citation
Vieux 12/03/2008, 17h17   #3
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

But I need it.

Rob Biedenharn wrote:
> On Mar 12, 2008, at 11:49 AM, Mario Ruiz wrote:
>
>> $bab="hiddenmodulevalue"
>> ...
>> Thanks.

> Uh, stop using global variables?
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 17h39   #4
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

On Mar 12, 2008, at 12:17 PM, Mario Ruiz wrote:
> But I need it.
>
> Rob Biedenharn wrote:
>> On Mar 12, 2008, at 11:49 AM, Mario Ruiz wrote:
>>
>>> $bab="hiddenmodulevalue"
>>> ...
>>> Thanks.

>> Uh, stop using global variables?
>>
>> -Rob




Do you really?

Perhaps you can describe your "need" and get a better answer, but why
not something like:

==> scenarios.rb <==
module Scenarios
module Default
def bab; "defaultmodulevalue"; end
end
module DefaultHidden
def bab; "hiddenmodulevalue"; end
end
module DefaultSecond
def bab; "secondmodulevalue"; end
end
end
__END__

==> these.rb <==
require 'scenarios'
class ThisOne
include Scenarios:efault
end

class ThatOne
include Scenarios:efaultSecond
end

puts "ThisOne has #{ThisOne.new.bab}"
puts "ThatOne has #{ThatOne.new.bab}"
__END__


-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com



  Réponse avec citation
Vieux 12/03/2008, 18h17   #5
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

so it's impossible to do it whithout declaring a method...
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 12/03/2008, 19h39   #6
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

On Mar 12, 2008, at 1:17 PM, Mario Ruiz wrote:

> so it's impossible to do it whithout declaring a method...


You still haven't defined what "it" really is. Perhaps with a better
description of what you're hoping to accomplish you could get a better
answer or hint.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com


  Réponse avec citation
Vieux 13/03/2008, 03h07   #7
Christopher Dicely
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

On Wed, Mar 12, 2008 at 10:17 AM, Mario Ruiz <mario@betware.com> wrote:
> so it's impossible to do it whithout declaring a method...


That depends what you want, which isn't clear. If you don't need to change the
values (and even if you do, since the constant-ness of Ruby constants isn't
enforced, though using them when you don't want something constant is
usually bad design), you can do without methods:

file:scenarios.rb
------------------
module Scenarios
module Default
BAB ="defaultmodulevalue"
end
module DefaultHidden
BAB ="hiddenmodulevalue"
end
module DefaultSecond
BAB ="secondmodulevalue"
end
end

file:mytest.rb
---------------
require 'scenarios.rb'
include Scenarios:efault
...
puts BAB

--------------

  Réponse avec citation
Vieux 13/03/2008, 10h52   #8
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Actually what I want is to include an scenario with all my global
variables and if I want another scenario I can include it and all the
global variables will change.
module Scenarios
module MyFirstSc
$web="http://www.elmundo.es"
$sqlSearch="select video from datab"
$user="Pep"
$password="xxx"
end
module MySecondSc
$web="http://www.elmundo.es"
$sqlSearch="select audiodefault from datab"
$user="Bee"
$password="2x8"
end
end

In my test cases I'll have in the first line for example:
include Scenarios::MySecondSc
and if I want to change the scenario I only have to change this line:
include Scenarios::MyFirstSc

How can I do it without including an extraline with a method?
I know I could do something like:
module Scenarios
module MyFirstSc
def getValues
$web="http://www.elmundo.es"
$sqlSearch="select video from datab"
$user="Pep"
$password="xxx"
end
end
...
end

and in the first line we'll write:
include Scenarios::MyFirstSc
Scenarios::MyFirstSc.getValues()


But I don't like this way I would like to do it in only one line... is
it possible???

Thanks for your time.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 13/03/2008, 12h36   #9
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Hi --

On Thu, 13 Mar 2008, Mario Ruiz wrote:

> Actually what I want is to include an scenario with all my global
> variables and if I want another scenario I can include it and all the
> global variables will change.
> module Scenarios
> module MyFirstSc
> $web="http://www.elmundo.es"
> $sqlSearch="select video from datab"
> $user="Pep"
> $password="xxx"
> end
> module MySecondSc
> $web="http://www.elmundo.es"
> $sqlSearch="select audiodefault from datab"
> $user="Bee"
> $password="2x8"
> end
> end
>
> In my test cases I'll have in the first line for example:
> include Scenarios::MySecondSc
> and if I want to change the scenario I only have to change this line:
> include Scenarios::MyFirstSc
>
> How can I do it without including an extraline with a method?
> I know I could do something like:
> module Scenarios
> module MyFirstSc
> def getValues
> $web="http://www.elmundo.es"
> $sqlSearch="select video from datab"
> $user="Pep"
> $password="xxx"
> end
> end
> ...
> end
>
> and in the first line we'll write:
> include Scenarios::MyFirstSc
> Scenarios::MyFirstSc.getValues()
>
>
> But I don't like this way I would like to do it in only one line... is
> it possible???


See Chris's answer: use constants, and include the module you want.
I've been programming in Ruby for 7.5 years and I have no clear memory
of ever having created a global variable. I imagine I have once or
twice, but it's very, very unlikely that you need to do it or that
it's the best way to do it in this case.


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

  Réponse avec citation
Vieux 13/03/2008, 13h21   #10
Morel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Mario Ruiz <mario@betware.com> wrote:

> n my test cases I'll have in the first line for example:
> include Scenarios::MySecondSc
> and if I want to change the scenario I only have to change this line:
> include Scenarios::MyFirstSc


I think this code will do what you require

module Scenarios

module Default
def self.included(mod)
super(mod)
$bab="defaultmodulevalue"
end
end

module DefaultHidden
def self.included(mod)
super(mod)
$bab="hiddenmodulevalue"
end
end

module DefaultSecond
def self.included(mod)
super(mod)
$bab="secondmodulevalue"
end
end
end

Module#included gets sent automatically by Ruby when you include a
module, so you do not need to invoke it explicitly.
--
Morel
  Réponse avec citation
Vieux 13/03/2008, 13h35   #11
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Thanks Morel I think this one could be the solution.
But I have a question for you...
Why do you need super(mod)??

Thanks
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 13/03/2008, 13h40   #12
Splenetico
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Mario Ruiz <mario@betware.com> wrote:

> Thanks Morel I think this one could be the solution.
> But I have a question for you...
> Why do you need super(mod)??


Because we are replacing (overriding) a previous version of
Module#included.

super(mod) ensures that whatever the previous version did continues to
get done.

--
lo Splenetico
  Réponse avec citation
Vieux 13/03/2008, 13h42   #13
Morel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Splenetico <splenetico@splenetico.net> wrote:

> Mario Ruiz <mario@betware.com> wrote:
>
> > Thanks Morel I think this one could be the solution.
> > But I have a question for you...
> > Why do you need super(mod)??

>
> Because we are replacing (overriding) a previous version of
> Module#included.
>
> super(mod) ensures that whatever the previous version did continues to
> get done.



OOps, wrong nick

--
Morel
  Réponse avec citation
Vieux 13/03/2008, 13h54   #14
Mario Ruiz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: including general variables in modules

Thanks everybody for your time and suggestions.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h51.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20454 seconds with 22 queries