我想向code
String 对象添加一个单例方法b = 'text'
。它应该能够引用a
本地范围内定义的散列。但我的尝试导致错误:
a = {'code'=>200, 'body'=>'text'}
b = a['body']
def b.code
return a['code']
end
p b.code
# => 'code': undefined local variable or method `a' for "text":String (NameError)
我怎样才能使这项工作?