Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Created March 14, 2016 16:12
Show Gist options
  • Select an option

  • Save tiagopog/7cd65e81e90735246dfd to your computer and use it in GitHub Desktop.

Select an option

Save tiagopog/7cd65e81e90735246dfd to your computer and use it in GitHub Desktop.
Ruby's define_method
class A
def fred
puts "In Fred"
end
def create_method(name, &block)
self.class.send(:define_method, name, &block)
end
define_method(:wilma) { puts "Charge it!" }
end
class B < A
define_method(:barney, instance_method(:fred))
end
a = B.new
a.barney
a.wilma
a.create_method(:betty) { p self }
a.betty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment