including general variables in modules

M

Mario Ruiz

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.
 
R

Rob Biedenharn

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
(e-mail address removed)
 
R

Rob Biedenharn

But I need it. :(



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
(e-mail address removed)
 
C

Christopher Dicely

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

--------------
 
M

Mario Ruiz

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.
 
D

David A. Black

Hi --

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
 
M

Mario Ruiz

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

Thanks
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top