I want to add a singleton method code
to the String object b = 'text'
. It should be able to refer to the hash a
defined in the local scope. But my attempt results in an error:
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)
How can I make this work?