Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimsynz/10336130 to your computer and use it in GitHub Desktop.
Save jimsynz/10336130 to your computer and use it in GitHub Desktop.
Monkey patching singleton methods...
m = Module.new { def a; :a; end }
# => #<Module:0x3cbea6af>
method = m.instance_method :a
# => #<UnboundMethod: #<Module:0x3cbea6af>#a>
o = Object.new
# => #<Object:0x590bcaf1>
o.define_singleton_method :a, method
# TypeError: bind argument must be an instance of #<Module:0x3cbea6af>
# from org/jruby/RubyModule.java:1521:in `define_method'
# from org/jruby/RubyKernel.java:1491:in `define_singleton_method'
# from (irb):4:in `evaluate'
# from org/jruby/RubyKernel.java:1121:in `eval'
# from org/jruby/RubyKernel.java:1521:in `loop'
# from org/jruby/RubyKernel.java:1284:in `catch'
# from org/jruby/RubyKernel.java:1284:in `catch'
# from /Users/jnh/.rvm/rubies/jruby-1.7.11/bin/irb:13:in `(root)'
m = Module.new { def a; :a; end }
# => #<Module:0x007f84ba85f2c8>
method = m.instance_method :a
# => #<UnboundMethod: #<Module:0x007f84ba85f2c8>#a>
o = Object.new
# => #<Object:0x007f84ba84b048>
o.define_singleton_method :a, method
# TypeError: bind argument must be a subclass of #<Module:0x007f84ba85f2c8>
# from (irb):4:in `define_singleton_method'
# from (irb):4
# from /Users/jnh/.rvm/rubies/ruby-1.9.3-p545/bin/irb:12:in `<main>'
m = Module.new { def a; :a; end }
# => #<Module:0x007fd183874d60>
method = m.instance_method :a
# => #<UnboundMethod: #<Module:0x007fd183874d60>#a>
o = Object.new
# => #<Object:0x007fd18385e0b0>
o.define_singleton_method :a, method
# => #<UnboundMethod: #<Module:0x007fd183874d60>#a>
o.a
# => :a
m = Module.new { def a; :a; end }
# => #<Module:0x000001010c46d8>
method = m.instance_method :a
# => #<UnboundMethod: #<Module:0x000001010c46d8>#a>
o = Object.new
# => #<Object:0x0000010109a9f0>
o.define_singleton_method :a, method
# => :a
o.a
# => :a
m = Module.new { def a; :a; end }
# => #<Module:0xfed4>
method = m.instance_method :a
# => #<UnboundMethod: #<Module:0xfed4>#a (defined in #<Module:0xfed4> at (irb):1)>
o = Object.new
# => #<Object:0xff40>
o.define_singleton_method :a, method
# => :a
o.a
# => :a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment