Now, I've been exposed to more ruby, and more metaprogramming in ruby, than in python - but this stil surprises/intrigues me.
What kind of metaprogramming do you find easier in python?
Now, I've been exposed to more ruby, and more metaprogramming in ruby, than in python - but this stil surprises/intrigues me.
What kind of metaprogramming do you find easier in python?
def m2():
print("nuovo")
object.new_method = m2
instead of redefining the class. It's similar to JavaScript. However I don't have a computer at hand now. I can't check the details and what could be done on Ruby.(No judgement from me on whether that's better/worse/about equal, just confirming.)
OTOH, if your problem is slightly different, I find the syntax to add methods to any class very practical and powerful, with of course the caveat that you can produce a maintainability nightmare if overused:
irb(main):001:0> a = 1
=> 1
irb(main):002:0> class Integer
irb(main):003:1> def hello
irb(main):004:2> puts "world"
irb(main):005:2> end
irb(main):006:1> end
=> :hello
irb(main):007:0> a.hello
world
=> nilI wouldn't recommend actually doing that, however.
If actually doing this (adding new method to a class) I think it's quite easy to re-open a class in ruby (see sibling comment(s)).
I can see adding methods actual instances (only) might be more work in ruby though.
What would be the typical use case for a special instance in a given scope, as opposed to a special class in a given scope?
Ed: i see qquark's so link address the technical part: define_singleton_method (so no need to open Object to add that...)