Help on phps $$ equivalent in ruby

i just cant find this anywhere, googling for a while and read up tons on
the docs but no joy, I must have overlooked this somewhere

how do I create the following in Ruby

php::

$text=“Something”
$$text=Someobject.new

thanks!

Arthur R.:

how do I create the following in Ruby

php::

$text=“Something”
$$text=Someobject.new

text = ‘Something’
eval “#{text} = Someobject.new”

— Shot

gr8, ty so much, i missed this as a noobie!

its not quite right, can you see what I am doing wrong

e.g. i’ve got a lot of fields - so a short version of my situation

fields = [[“somthing”]]

fields.each{|x|
eval “#{x[0]} = Someclass::Somemethod.new”
}

puts something.text=“asdfasdfasdf”

text is method from the Somthingclass that should work, but I get
undefined variable for something.text

got it, working added an @ to

eval "@#{x[0]} = …

it worked

On Aug 31, 2009, at 17:23, Arthur R. wrote:

puts something.text=“asdfasdfasdf”

text is method from the Somthingclass that should work, but I get
undefined variable for something.text

Don’t use eval this way, it’s wrong, wrong, wrong. Use a Hash.

result = {}

fields.each do |field|
result[field] = SomeClass.new
end

ok, cheers,

On 01.09.2009 02:32, Arthur R. wrote:

iyo, why wouldnt this be good, not that I know better,but need to learn

eval has some security implications and is slow compared to “regular”
code Use it only as a last resort for things that cannot be solved
otherwise (e.g. generated code).

Cheers

robert

Arthur R.:

fields = [[“somthing”]]

fields.each{|x|
eval “#{x[0]} = Someclass::Somemethod.new”
}

puts something.text=“asdfasdfasdf”

text is method from the Somthingclass that should
work, but I get undefined variable for something.text

Others answered why it’s bad to use eval in this scenario (as it is
is most scenarios where it can be avoided), I’ll answer why the above
doesn’t work: by calling eval inside the each block you’re creating
block-local variables which disappear right after you leave the block
(i.e., on the eval’s closing curly brace).

As for why using eval “@#{x[0]} = …” works – in this example you’re
creating instance variables on the current object, and these live on
after the block ends.

— Shot

iyo, why wouldnt this be good, not that I know better,but need to learn

thanks, really good, got the code working without eval, nice to know for
future about the blocks! :slight_smile: learning more each day , soon I’ll be able
to write some decent Ruby code!! thanks again

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs