[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regarding Constants

Harnish Botadra

7/24/2007 8:21:00 PM

Hi,

I am having some difficulty in the use of constants in Ruby. I've a
module:

eg.
-------------------
Module Constants

MY_CONST = "hello"

end
-------------------

I include this module in my model. Further, the name of the constant
(MY_CONST) would be passed in a variable say 'var'. My question, is how
do I access the value of the constant from the constant name in 'var'?

Something like, :: + "{#var}" or similar? Any help greatly appreciated.
Thanks.

Regards,
Harnish
--
Posted via http://www.ruby-....

4 Answers

skye.shaw

7/25/2007 4:26:00 AM

0

On Jul 24, 1:21 pm, Harnish Botadra <harnish_...@yahoo.com> wrote:
> Hi,
>
> I am having some difficulty in the use of constants in Ruby. I've a
> module:
>
> eg.
> -------------------
> Module Constants

Use lowercase "m"

> I include this module in my model. Further, the name of the constant
> (MY_CONST) would be passed in a variable say 'var'. My question, is how
> do I access the value of the constant from the constant name in 'var'?

module Constants
GRR="burr!"
end

class Bs
include Constants

def self.get_constant(name)
self.const_get(name.to_sym)
end
end

p Bs.get_constant("GRR")


Why do you need to access constants this way?

FireAphis@gmail.com

7/25/2007 7:17:00 AM

0

On Jul 24, 11:21 pm, Harnish Botadra <harnish_...@yahoo.com> wrote:
> Hi,
>
> I am having some difficulty in the use of constants in Ruby. I've a
> module:
>
> eg.
> -------------------
> Module Constants
>
> MY_CONST = "hello"
>
> end
> -------------------
>
> I include this module in my model. Further, the name of the constant
> (MY_CONST) would be passed in a variable say 'var'. My question, is how
> do I access the value of the constant from the constant name in 'var'?
>
> Something like, :: + "{#var}" or similar? Any help greatly appreciated.
> Thanks.
>
> Regards,
> Harnish
> --
> Posted viahttp://www.ruby-....

If I understand you correctly then:

Module Constants
MY_CONST = "hello"
end

def get_constant_value_by_name(var)
Constants.const_get(var)
end

get_constant_value_by_name("MY_CONST")
=> "hello"

Hope this helps,
FireAphis

FireAphis@gmail.com

7/25/2007 7:19:00 AM

0

On Jul 25, 10:17 am, "FireAp...@gmail.com" <FireAp...@gmail.com>
wrote:
> On Jul 24, 11:21 pm, Harnish Botadra <harnish_...@yahoo.com> wrote:
>
>
>
> > Hi,
>
> > I am having some difficulty in the use of constants in Ruby. I've a
> > module:
>
> > eg.
> > -------------------
> > Module Constants
>
> > MY_CONST = "hello"
>
> > end
> > -------------------
>
> > I include this module in my model. Further, the name of the constant
> > (MY_CONST) would be passed in a variable say 'var'. My question, is how
> > do I access the value of the constant from the constant name in 'var'?
>
> > Something like, :: + "{#var}" or similar? Any help greatly appreciated.
> > Thanks.
>
> > Regards,
> > Harnish
> > --
> > Posted viahttp://www.ruby-....
>
> If I understand you correctly then:
>
> Module Constants
> MY_CONST = "hello"
> end
>
> def get_constant_value_by_name(var)
> Constants.const_get(var)
> end
>
> get_constant_value_by_name("MY_CONST")
> => "hello"
>
> Hope this helps,
> FireAphis

Of course, as Skye pointed out, Module should be lowercase.
Yaniv

FireAphis@gmail.com

7/25/2007 7:59:00 AM

0

On Jul 25, 10:17 am, "FireAp...@gmail.com" <FireAp...@gmail.com>
wrote:
> On Jul 24, 11:21 pm, Harnish Botadra <harnish_...@yahoo.com> wrote:
>
>
>
> > Hi,
>
> > I am having some difficulty in the use of constants in Ruby. I've a
> > module:
>
> > eg.
> > -------------------
> > Module Constants
>
> > MY_CONST = "hello"
>
> > end
> > -------------------
>
> > I include this module in my model. Further, the name of the constant
> > (MY_CONST) would be passed in a variable say 'var'. My question, is how
> > do I access the value of the constant from the constant name in 'var'?
>
> > Something like, :: + "{#var}" or similar? Any help greatly appreciated.
> > Thanks.
>
> > Regards,
> > Harnish
> > --
> > Posted viahttp://www.ruby-....
>
> If I understand you correctly then:
>
> Module Constants
> MY_CONST = "hello"
> end
>
> def get_constant_value_by_name(var)
> Constants.const_get(var)
> end
>
> get_constant_value_by_name("MY_CONST")
> => "hello"
>
> Hope this helps,
> FireAphis

Of course, as Skye pointed out Module should be lowercase.
You can see that there's a slight difference between my and Skye's
implementation. That's because 'const_get' can receive either a symbol
or a string. I am personally not familiar with to_sym method. But if I
want to pass a symbol instead of a string I'd use 'var.intern'. That
is
Constants.const_get(var.intern)
which is exactly the same as
Constants.const_get(var)

FireAphis