including general variables in modules

Discussion in 'Ruby' started by Mario Ruiz, Mar 12, 2008.

  1. Mario Ruiz

    Mario Ruiz Guest

    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::Default
    ...
    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/.
    Mario Ruiz, Mar 12, 2008
    #1
    1. Advertising

  2. 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::Default
    > ...
    > 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 Biedenharn, Mar 12, 2008
    #2
    1. Advertising

  3. Mario Ruiz

    Mario Ruiz Guest

    Mario Ruiz, Mar 12, 2008
    #3
  4. 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::Default
    end

    class ThatOne
    include Scenarios::DefaultSecond
    end

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


    -Rob

    Rob Biedenharn http://agileconsultingllc.com
    Rob Biedenharn, Mar 12, 2008
    #4
  5. Mario Ruiz

    Mario Ruiz Guest

    Mario Ruiz, Mar 12, 2008
    #5
  6. 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 Biedenharn, Mar 12, 2008
    #6
  7. On Wed, Mar 12, 2008 at 10:17 AM, Mario Ruiz <> 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::Default
    ...
    puts BAB

    --------------
    Christopher Dicely, Mar 13, 2008
    #7
  8. Mario Ruiz

    Mario Ruiz Guest

    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/.
    Mario Ruiz, Mar 13, 2008
    #8
  9. 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!
    David A. Black, Mar 13, 2008
    #9
  10. Mario Ruiz

    Mario Ruiz Guest

    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/.
    Mario Ruiz, Mar 13, 2008
    #10
  11. Mario Ruiz

    Mario Ruiz Guest

    Mario Ruiz, Mar 13, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Julia Goolia
    Replies:
    5
    Views:
    398
    Matthias Baas
    Jul 18, 2003
  2. Paulo Jabardo

    Inconsistent(?) when including modules

    Paulo Jabardo, Jul 7, 2005, in forum: Ruby
    Replies:
    2
    Views:
    94
    Robert Klemme
    Jul 8, 2005
  3. Tim Olsen

    modules including other modules

    Tim Olsen, May 29, 2007, in forum: Ruby
    Replies:
    4
    Views:
    124
    Adriano Mitre
    Jan 20, 2011
  4. Aryk Grosz
    Replies:
    3
    Views:
    104
    Aryk Grosz
    Jun 27, 2007
  5. Mr Hyde
    Replies:
    3
    Views:
    154
    Mr Hyde
    May 18, 2005
Loading...

Share This Page