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!

···

--
Posted via http://www.ruby-forum.com/.

Arthur Rats:

how do I create the following in Ruby

php::

$text="Something"
$$text=Someobject.new

text = 'Something'
eval "#{text} = Someobject.new"

— Shot

···

--
a :message to you, Ruby

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

got it, working added an @ to

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

it worked

···

--
Posted via http://www.ruby-forum.com/.

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

result = {}

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

···

On Aug 31, 2009, at 17:23, Arthur Rats wrote:

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

Arthur Rats:

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

···

--

The Ruby community should proceed with all deliberate
speed towards ISO standardization of the language.

Yeah, look what it did to Forth.

Don’t just say it, show it. http://vividpicture.com/aleks/atari/forth.jpg
        [M. Edward (Ed) Borasky, Matt Lawrence, Gregory Brown, ruby-talk]

ok, cheers,

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 01.09.2009 02:32, Arthur Rats wrote:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/