Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View arup_meth_var.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/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
Something went wrong with that request. Please try again.