3 ms·
> don't provide anything more than classes do in nice dynamic languages How would you do in-place method mutation in something like Python or Ruby?
by wwweston 15y ago
> don't provide anything more than classes do in nice dynamic languages
How would you do in-place method mutation in something like Python or Ruby?
- chc 15y agoNot quite sure what you mean, but I think it's something like this: a = "hello" puts a.size #=> 5 class <<a def size 1 end end puts a.size #=> 1 Am I on the right track?
- judofyr 15y agoIn 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!" }
- masklinn 15y agoCould you explain what you mean by "in-place method mutation"? With an example in javascript maybe? Is it just about adding or replacing a method on a type (or an instance), or do you mean something more complex?