Skip to content

Instantly share code, notes, and snippets.

@jay16
Last active August 29, 2015 14:00
Show Gist options
  • Save jay16/11167872 to your computer and use it in GitHub Desktop.
Save jay16/11167872 to your computer and use it in GitHub Desktop.
[root@solife tools]# ruby method_missing.rb
method missing stack called..
define_method: title
"title"
"title"
no mater what you do, i say: hello
[root@solife tools]# cat method_missing.rb
class A
def method_missing(method_id, *args)
puts "method missing stack called.."
if method_id.to_s =~ /^find_by_(.*?)$/
name = $1
puts "define_method: " + name
self.class.send(:define_method, method_id) do
name
end
send(method_id, *args)
else
super
end
end
end
class Object
def method_missing(method_id, *args)
puts "no mater what you do, i say: " + method_id.to_s
end
end
obj = A.new
p obj.find_by_title
p obj.find_by_title
1.hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment