Skip to content

Instantly share code, notes, and snippets.

@pedrocr
Created August 30, 2013 14:21
Show Gist options
  • Save pedrocr/6390369 to your computer and use it in GitHub Desktop.
Save pedrocr/6390369 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Test
def self.hello(name)
puts "hello #{name}"
end
def hello2(name)
puts "hello #{name}"
end
def self.my_create_method(name)
define_method(name) do
yield "bar"
end
end
#This works
my_create_method(:foo) do |obj|
self.hello(obj)
end
#This doesn't work
my_create_method(:foo2) do |obj|
hello2(obj)
end
end
t = Test.new
t.foo # "hello bar"
t.foo2 #=> testcase.rb:15:in `block in <class:Test>': undefined method `hello' for Test:Class (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment