# Regarding Constants

**URL:** <https://www.ruby-forum.com/t/regarding-constants/110284>\
**Category:** Ruby\
**Created:** [July 24, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/regarding-constants/110284 "2007-07-24T22:21:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 24, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/regarding-constants/110284/1 "2007-07-24T22:21:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/regarding-constants/110284/2 "2007-07-26T00:46:02Z")

</div>

On Jul 25, 10:17 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > * * *
> 
> Module Constants  
> 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

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/regarding-constants/110284/3 "2007-07-26T00:46:27Z")

</div>

On Jul 24, 1:21 pm, Harnish B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/regarding-constants/110284/4 "2007-07-26T00:45:57Z")

</div>

On Jul 24, 11:21 pm, Harnish B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Regards, Harnish
> 
> Posted viahttp://www.ruby-forum.com/.

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

---

<div class="post-metadata">

**Author:** ![hbotadra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e9bcb4/32.png) [@hbotadra](https://www.ruby-forum.com/u/hbotadra)\
**Post date:** [September 25, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/regarding-constants/110284/5 "2007-09-25T23:01:20Z")

</div>

On Jul 25, 10:17 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > * * *
> 
> Module Constants  
> Hope this helps,  
> FireAphis

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