-
-
Save aruprakshit/ad528d6469db2d31a481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Wrong | |
def method_missing(m, *) | |
if m =~ /\Ahello_(.+)\z/ | |
puts "Hello, #{$1.capitalize}" | |
else | |
super | |
end | |
end | |
end | |
w = Wrong.new | |
w.hello_world | |
p w.respond_to? :hello_world | |
p defined? w.hello_world | |
begin | |
p w.method(:hello_world) | |
rescue NameError | |
puts "not defined!" | |
end | |
puts | |
class Right | |
def method_missing(m, *) | |
if m =~ /\Ahello_(.+)\z/ | |
puts "Hello, #{$1.capitalize}" | |
else | |
super | |
end | |
end | |
def respond_to_missing?(m, include_all) | |
m =~ /\Ahello_(.+)\z/ || super | |
end | |
end | |
r = Right.new | |
r.hello_world | |
p r.respond_to? :hello_world | |
p defined? r.hello_world | |
begin | |
p r.method(:hello_world) | |
rescue NameError | |
puts "not defined!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment