I have a variable var = "some_name"
and I would like to create a new object and assign it to some_name
. How can I do it? E.g.
var = "some_name"
some_name = Struct.new(:name) # I need this
a = some_name.new('blah') # so that I can do this.
I have a variable var = "some_name"
and I would like to create a new object and assign it to some_name
. How can I do it? E.g.
var = "some_name"
some_name = Struct.new(:name) # I need this
a = some_name.new('blah') # so that I can do this.
Speaking of ruby 2.2.x it is true that you can't create local variables programatically in current context/binding.. but you can set variables in some particular binding you have a handle of.
Interesting here is that calls to
binding
give you a new binding each time. So you need to get a handle of the binding you are interested in and then eval in it's context once desired variables are set.How is this useful? For example I want to evaluate ERB and writing ERB is much nicer if you can use
<%= myvar %>
instead of<%= opts[:myvar] %>
or something like that.To create a new binding I'm using a module class method (I'm sure somebody will correct me how to call this properly, in java I'd call it a static method) to get a clean binding with particular variables set:
Now you have a binding with only the desired variables. You can use it for nicer controlled evaluation of ERB or other (possibly third party) trusted code (this is not a sandbox of any kind). It's like defining an interface.
update: A few additional notes about bindings. Place you create them also affects the availability of methods and Constants resolution. In the above example I create a reasonably clean binding. But if I want to make available the instance methods of some object, I could create a binding by a similar method but within the class of that object. e.g.
Now my
my_object.not_so_clean_binding
will allow code to call#my_instance_method
onmy_object
object. In the same way, you can call for exampleSomeOtherClass.new
in code using this binding instead ofMyRootModule::SomeOtherClass.new
. So there is sometimes more consideration needed when creating a binding than just local variables. HTHIt is true what others wrote that you cannot dynamically declare true variable in a local context. However you can achieve similar functionality with object attributes and since in the Ruby world everything is an object (even main context) you can easily extend those objects with new attributes. Of corse, this operation can be done dynamically. Let's examine this approach.
Firstly, let's look at the main scope with
irb
.As you can see now,
main
is truly an object. Objects can have attributes which have same indirection property as variables. Normally, when declaring new class we would useattr_accessor
method butmain
is already an instantiated object thus we cannot declare new attributes directly. Here module mixins come for rescue.Now you see that
main
object was tainted with new module that introduced new attributefoo
. For further inspection you can runmethods
to see thatmain
now have two more methodsfoo
andfoo=
.To simplify this operation I wrote metaxa gem which I highly encourage you to check out. This is example of how to use it.
Although, as others have pointed out, you cannot dynamically create local variables in Ruby, you can simulate this behavior to some degree using methods:
Prints:
Some libraries combine this technique with
instance_exec
to expose what appear to be local variables inside a block:Prints: 3
Be aware though that the abstraction is by no means perfect:
Results in:
undefined method `+' for nil:NilClass
. This is becausea=
defines an actual local variable, initialized tonil
, which takes precedence over the methoda
. Thena.+(1)
gets called, andnil
doesn't have a+
method, so an error is thrown.So while this method is pretty useful for simulating read-only local variables, it doesn't always work well when you try to reassign the variable inside the block.
You cannot dynamically create local variables in Ruby 1.9+ (you could in Ruby 1.8 via
eval
):They can be used within the eval-ed code itself, though:
Ruby 2.1 added
local_variable_set
, but that cannot create new local variables either:This behavior cannot be changed without modifying Ruby itself. The alternative is to instead consider storing your data within another data structure, e.g. a Hash, instead of many local variables:
Note that both
eval
andlocal_variable_set
do allow reassigning an existing local variable: