3 ms·
# Add method foo only to instance x. (x = Object.new).instance_eval { def foo ; puts 'bar' end }
by ballard 14y ago
# Add method foo only to instance x.
(x = Object.new).instance_eval { def foo ; puts 'bar' end }
- caiusdurling 14y agoI find a better way of doing this is to use a module. x = Object.new x.extend Module.new { def foo; puts 'bar'; end } x.foo # >> bar
- Spakman 14y agoAnother option (that I prefer): x = Object.new def x.foo puts 'bar' end