Skip to content

Instantly share code, notes, and snippets.

@maxjacobson
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save maxjacobson/03ff34b261bfd04d847b to your computer and use it in GitHub Desktop.

Select an option

Save maxjacobson/03ff34b261bfd04d847b to your computer and use it in GitHub Desktop.
class Dog
def initialize(name)
@name = name
end
def to_s
"#<Dog name=#{@name}>"
end
def bark
"WOOF"
end
end
Dog.instance_method(:bark).bind(Dog.new('milo')).call #=> "WOOF"
# how?? what??
unbound_bark_method = Dog.instance_method(:bark) #=> #<UnboundMethod: Dog#bark>
dog = Dog.new('Milo') #=> #<Dog name=Milo>
bark_method = unbound_bark_method.bind(dog) #=> #<Method: Dog#bark>
bark_method.receiver #=> #<Dog name=Milo>
bark_method.owner #=> Dog
bark_method.call #=> "WOOF"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment