4 ms·
In Ruby: foo = "Hello" def foo.to_s; "Hello world!" end Because "def" introduces a new local variable scope, you'll have to use #define_singleton_meth
by judofyr 14y ago
In Ruby:
foo = "Hello"
def foo.to_s; "Hello world!" end
Because "def" introduces a new local variable scope, you'll have to use #define_singleton_method if you want to close over variables:
foo = "Hello"
old = foo.method(:to_s)
foo.define_singleton_method(:to_s) { old.call + " world!" }