# Dynamic creation of variables

**URL:** <https://www.ruby-forum.com/t/dynamic-creation-of-variables/210084>\
**Category:** Ruby\
**Created:** [July 30, 2011, 3:18am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-variables/210084 "2011-07-30T03:18:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 30, 2011, 3:18am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-variables/210084/1 "2011-07-30T03:18:29Z")

</div>

Hi everyone!  
I am building a interactive shell for chemistry related tasks and am  
stuck.  
The program loads all the elements of the periodic table and there  
properties, into an array of objects.

Eg:

elements[0] is the element hydrogen.  
puts elements[0].name #=\> “hydrogen”  
puts elements[0].mass #=\> 1.0079  
puts elements[0].symbol #=\> “H”

I am also making them available by the symbol as follows

@elements.each do |e|  
instance\_variable\_set("@" + e.symbol, e)  
end

thus:

puts elements[0].name #=\> “hydrogen”  
puts @H.name #=\> “hydrogen”

But i would prefer it if the elements were loaded up as normal variables  
without the ‘@’ in front.

is this possible?

Thanks in advance, Tom

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [July 30, 2011, 4:13am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-variables/210084/2 "2011-07-30T04:13:29Z")

</div>

> puts elements[0].name #=\> “hydrogen”  
> puts @H.name #=\> “hydrogen”
> 
> But i would prefer it if the elements were loaded up as normal variables  
> without the ‘@’ in front.

I would recommend making a module, then setting module constants for  
your element symbols to be mapped to objects. Here is a sample I threw  
together:

module Chemistry  
class Element  
attr\_reader :name  
attr\_reader :mass  
attr\_reader :symbol

def initialize(name,mass,symbol)  
@name = name  
@mass = mass  
@symbol = symbol  
end  
end  
end

elements = [  
Chemistry::Element.new(“hydrogen”, 1.0079, “H”),  
Chemistry::Element.new(“lithium”, 6.941, “Li”)  
]

elements.each do |element|  
Chemistry.const\_set(element.symbol, element)  
end

include Chemistry  
puts Li.name

Please note the include Chemistry which makes the “Li.name” access  
possible. This has the advantage of being able to use these constants in  
other scripts where someone might want to use the H constant for a  
different purpose, then they can access it like this:

Chemistry::Li.name

Regards,  
Chris W.  
Twitter: @cwgem

---

<div class="post-metadata">

**Author:** ![thomasmaas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@thomasmaas](https://www.ruby-forum.com/u/thomasmaas)\
**Post date:** [July 30, 2011, 4:27am UTC](https://www.ruby-forum.com/t/dynamic-creation-of-variables/210084/3 "2011-07-30T04:27:23Z")

</div>

Thanks for the quick reply!

This solution is very elegant, especially the way the variables are  
kept seperated in the module.
