Discussion: string to symbol
Afficher un message
Vieux 06/11/2007, 16h32   #7
Marc Heiler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

> If you wish. Note that you can't create constants from within methods.

I think what you mean is the dynamic constant assignment like so
def hi
ABC = "hi"
end

But you can generate them programmatically like so:


def test(arg)
_ = Object.const_set(arg.capitalize, "this is the constant
"+arg.capitalize)
end

test 'foo'
test 'bar'

puts Foo # "this is the constant Foo"
puts Bar # "this is the constant Bar"


I love constants btw I even abuse them and dont care that I do!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
 
Page generated in 0,04270 seconds with 9 queries