访问本地变量我是新来的,但我有以下代码:使用字符串按名称
when /^read (.+)$/
puts "Reading #{$1}:"
puts $1.description.downcase
我想用$ 1中,我可以调用方法的变量,目前口译员返回"NoMethodError: undefined method 'description' for "Door":String"
。
编辑:
例如:
door = Item.new(:name => "Door", :description => "a locked door")
key = Item.new(:name => "Key", :description => "a key" )
的可能的复制_ [使用字符串在Ruby on Rails的运行时间的变量(HTTP:/ /stackoverflow.com/questions/881239/using-a-string-as-a-variable-at-run-time-in-ruby-on-rails)_或_ [使用字符串作为函数名称](http: //stackoverflow.com/questions/877882/using-a-string-as-function-name/878174)_。 – Phrogz 2011-05-07 12:23:06