top | item 20903326

(no title)

qquark | 6 years ago

This may be what you're looking form, to add a method to a single instance, although the syntax is clearly not as smooth: https://stackoverflow.com/questions/1887845/add-method-to-an...

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
  => nil

discuss

order

No comments yet.