Dear Arup Rakshit,

I think this is because _method_ lookup is different from _variable_
lookup in Ruby.

Try this on a file (because inside pry or irb is hard to deal with
method_missing).

https://gist.github.com/abinoam/8892399

#!/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 =3D "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

Abinoam Jr.

On Sat, Feb 8, 2014 at 8:15 PM, Arup Rakshit <lists / ruby-forum.com> wrote:
> F=F6ldes L=E1szl=F3 wrote in post #1136082:
>> Edoardo Rossi wrote in post #1136041:
>
>> [1] pry(main)> puts a if a =3D 0.zero?
>> NameError: undefined local variable or method `a' for main:Object
>> from (pry):1:in `__pry__'
>> [2] pry(main)> a
>> =3D> true
>> [3] pry(main)>
>>
>> If anything that is defined gets into this inner scope and anything that
>> is defined (or modified) inside the inner scope gets out, then this is
>> not really a scope...
>
> Another example, which doesn't create a noise like local variable in my
> first post.
>
> (arup~>~)$ pry --simple-prompt
>>> puts meth if eval('def meth;12;end').nil?
> 12
> =3D> nil
>>>
>
> --
> Posted via http://www.ruby-forum.com/.