Trying to reference a hash using a string value in Ruby

Posted by Jim on Stack Overflow See other posts from Stack Overflow or by Jim
Published on 2010-05-26T14:22:05Z Indexed on 2010/05/26 14:31 UTC
Read the original article Hit count: 266

Filed under:
|
|
|
|

How would I refer to a hash using the value of a string - i.e.

#!/usr/bin/env ruby
foo = Hash.new
bar = "foo"
"#{bar}"["key"] = "value"

results in

 foo:5:in `[]=': string not matched (IndexError)
 from foo:5

How do I use the value of bar (foo) to reference the hash named foo? Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about string