Skip to content

Instantly share code, notes, and snippets.

@abinoam
Created February 9, 2014 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abinoam/8892399 to your computer and use it in GitHub Desktop.
Save abinoam/8892399 to your computer and use it in GitHub Desktop.
#!/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