# Newbie question: how to create variable

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417>\
**Category:** Ruby\
**Created:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417 "2006-08-15T14:16:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417/1 "2006-08-15T14:16:57Z")

</div>

Hi,

how to create variable in the method that accepts string as name of the  
variable and the object?

Something like this:

class InterpreterCallback  
def set\_object(name, value)  
#do something here  
end  
end

$callback = InterpreterCallback.new()  
$callback.set\_object(“var”, 3)

puts var

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417/2 "2006-08-15T14:17:18Z")

</div>

Karel M. wrote:

> Hi,
> 
> how to create variable in the method that accepts string as name of the variable and the object?
> 
> Something like this:
> 
> class InterpreterCallback  
> def set\_object(name, value)  
> #do something here

instance\_variable\_set("@#{name}", value)

> ```
> end
> 
> ```
> 
> end
> 
> $callback = InterpreterCallback.new()  
> $callback.set\_object(“var”, 3)
> 
> puts var
> 
> > > > 3

Alternatively you can use an OpenStruct.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417/3 "2006-08-15T14:17:53Z")

</div>

On Aug 11, 2006, at 12:55 PM, Robert K. wrote:

> > $callback = InterpreterCallback.new()  
> > $callback.set\_object(“var”, 3)  
> > puts var
> > 
> > > > > 3

Depending on what you’re trying to do, you might be able to use a  
simple eval statement:  
irb(main):001:0\> name=“var”  
=\> “var”  
irb(main):002:0\> value=“3”  
=\> “3”  
irb(main):003:0\> eval “#{name} = value”  
=\> “3”  
irb(main):004:0\> var  
=\> “3”

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417/4 "2006-08-15T14:17:56Z")

</div>

Hi –

On Sat, 12 Aug 2006, Mr Pinto wrote:

> > > end  
> > > irb(main):002:0\> value=“3”  
> > > =\> “3”  
> > > irb(main):003:0\> eval “#{name} = value”  
> > > =\> “3”  
> > > irb(main):004:0\> var  
> > > =\> “3”

That only works in irb. With ruby you’ll get:

ruby -e ‘name=“var”; value=“3”; eval("#{name}=#{value}"); var’  
-e:1: undefined local variable or method `var’ for main:Object  
(NameError)

David

---

<div class="post-metadata">

**Author:** ![kajism](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kajism](https://www.ruby-forum.com/u/kajism)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-create-variable/102417/5 "2006-08-15T14:18:43Z")

</div>

> > > > end  
> > > > irb(main):002:0\> value=“3”  
> > > > (NameError)
> 
> David

None of this actually works quite well for me. I am using SWIG directors  
to embed ruby (and python, perl) into C# application trough C++ layer.  
value actually wraps C++ object . InterpreterCallback actually extends  
c++ class and I am calling set\_object() from C++ - set\_object(“editor”,  
getEditor() ) - “editor” then can be used in the global namespace for  
scripting convenience:

editor.DrawGrid

I actually get it working with

```
def set_object(var, value)
  eval("def #{var}; $#{var}; end; proc{|_v| $#{var} = _v}", 

```

TOPLEVEL\_BINDING).call(value)  
end

but it goes through global variable. I believe there is simpler way how  
to do this.  
Kind regards,  
Karel
