Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Global constants
Kless
8/18/2008 8:20:00 AM
I want set up some constants that can be accesed since another
classes.
Which is the best way? Using a singleton to access to variables? (as
if were global vars.)?
2 Answers
Michael Morin
8/18/2008 9:19:00 AM
0
Kless wrote:
> I want set up some constants that can be accesed since another
> classes.
>
> Which is the best way? Using a singleton to access to variables? (as
> if were global vars.)?
>
You can use a module for this. The classes don't necessarily have to be
in the module either, if outside the module they can address the
constant as MyModule::CONSTANT or the classes can include MyModule if it
makes sense to do so.
module MyModule
CONSTANT = "test"
class MyClass1
def print_test
puts CONSTANT
end
end
class MyClass2
def say_something
puts CONSTANT
end
end
end
MyModule::MyClass1.new.print_test
MyModule::MyClass2.new.say_something
--
Michael Morin
Guide to Ruby
http://ruby....
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company
Kless
8/18/2008 5:00:00 PM
0
Thank you for your help and the fast reply.
I come from Python, a great language, but I'm loving Ruby.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Global constants
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password