Created
February 9, 2014 00:21
-
-
Save abinoam/8892399 to your computer and use it in GitHub Desktop.
In response to https://www.ruby-forum.com/topic/4422634
This file contains 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
#!/usr/bin/env ruby | |
# If no local variable is found, | |
# it begins the method lookup that ends calling | |
# the method_missing chain until the last one raises the error | |
begin | |
p undefined_name if true | |
rescue | |
puts "Raised an error" | |
else | |
puts "No error at all" | |
end | |
def method_missing(method, *args) | |
"I'm a method" | |
end | |
# When no method is found, | |
# It hits the method_missing and do what is defined on it | |
# so... returns "I'm a method" without raising any errors. | |
puts | |
begin | |
p undefined_name if true | |
rescue | |
puts "Raised an error" | |
else | |
puts "No error at all" | |
end | |
undefined_name = "I'm a local-variable" | |
# Now a local-variable with the same name is found (before the method) | |
puts | |
begin | |
p undefined_name if true | |
rescue | |
puts "Raised an error" | |
else | |
puts "No error at all" | |
end | |
# If we need to disambiguate it, for the method we need to prepend the object will be sending it to. | |
# self.undefined_name vs. undefined_name | |
puts | |
begin | |
p self.undefined_name if true | |
rescue | |
puts "Raised an error" | |
else | |
puts "No error at all" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment