newbie question: how to create variable

K

Karel Michek

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
 
R

Robert Klemme

Karel said:
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

Alternatively you can use an OpenStruct.

Kind regards

robert
 
D

dblack

Hi --

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"

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

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
K

Karel Michek

------------ P=F9vodn=ED zpr=E1va ------------
Od: <[email protected]>
P=F8edm=ECt: Re: newbie question: how to create variable
Datum: 12.8.2006 14:27:31
----------------------------------------
Hi --

imple eval


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

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


David

None of this actually works quite well for me. I am using SWIG director=
s to embed ruby (and python, perl) into C# application trough C++ layer=
value actually wraps C++ object . InterpreterCallback actually extend=
s c++ class and I am calling set_object() from C++ - set_object("editor=
", getEditor() ) - "editor" then can be used in the global namespace fo=
r scripting convenience:

editor.DrawGrid

I actually get it working with

def set_object(var, value)
eval("def #{var}; $#{var}; end; proc{|_v| $#{var} =3D _v}", TOPLE=
VEL_BINDING).call(value)
end

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,480
Latest member
CrazyMember

Latest Threads

Top