Skip to content

Instantly share code, notes, and snippets.

@jbr
Created May 19, 2014 21:09
Show Gist options
  • Save jbr/7bdc8ab6fa0dd3e2d8f2 to your computer and use it in GitHub Desktop.
Save jbr/7bdc8ab6fa0dd3e2d8f2 to your computer and use it in GitHub Desktop.
bind and define rbx vs 2.1.2
class A
def method_from_a() :from_a end
end
class B < A
def method_from_b() :from_b end
end
module M
def method_from_m() :from_m end
end
module N; end
A.send :define_method, :method_from_b, B.instance_method(:method_from_b) rescue puts "can't define a method from subclass onto parent"
B.send :define_method, :method_from_a, A.instance_method(:method_from_a) rescue puts "can't define a method from parent onto subclass"
A.send :define_method, :method_from_m, M.instance_method(:method_from_m) rescue puts "can't define a method from module onto a class"
N.send :define_method, :method_from_m, M.instance_method(:method_from_m) rescue puts "can't define a method from module onto another module"
N.send :define_method, :method_from_a, A.instance_method(:method_from_a) rescue puts "can't define a method from class onto a module"
A.instance_method(:method_from_a).bind(B.new) rescue puts "can't bind a method from parent class to subclass"
B.instance_method(:method_from_b).bind(A.new) rescue puts "can't bind a method from subclass on parent"
M.instance_method(:method_from_m).bind(A.new) rescue puts "can't bind a method from module to class"
M.instance_method(:method_from_m).bind(N) rescue puts "can't bind a method from module to another module"
A.instance_method(:method_from_a).bind(N) rescue puts "can't bind a method from class to module"
#REFERENCE
# rvm use 2.1.2; ruby in-a-bind.rb
# Using /Users/jbr/.rvm/gems/ruby-2.1.2
# can't define a method from subclass onto parent
# can't define a method from class onto a module
# can't bind a method from subclass on parent
# can't bind a method from class to module
# rvm use rbx; ruby in-a-bind.rb
# Using /Users/jbr/.rvm/gems/rbx-2.2.6
# can't bind a method from subclass on parent OK
# can't bind a method from module to class !!!
# can't bind a method from module to another module !!!
# can't bind a method from class to module OK
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment