On Wed, 6 Oct 2004 23:49:33 +0900, Joachim Wuttke
<joachim_wuttke / ph.tum.de> wrote:
> => system complains "no such method"
> 
> Formally, the system is right: I forgot to declare the read
> access method for the variable pos.
> 
> But:
> (1) the system should tell me which method in the above
> chain causes the problem

irb(main):004:0> e = Object.new
=> #<Object:0x28c6f50>
irb(main):005:0> e.pos
NoMethodError: undefined method `pos' for #<Object:0x28c6f50>
        from (irb):5

Now, it's impossible to tell which object returned is causing a problem, really:

irb(main):012:0> e = OpenStruct.new
=> <OpenStruct>
irb(main):013:0> e.f = OpenStruct.new
=> <OpenStruct>
irb(main):014:0> e.f.g = OpenStruct.new
=> <OpenStruct>
irb(main):015:0> e.f.g.h = Object.new
=> #<Object:0x28db698>
irb(main):016:0> e.f.g.h.pos
NoMethodError: undefined method `pos' for #<Object:0x28db698>
        from (irb):16

But as a smart programmer, you'll note that it's whatever object is
calling "pos" -- because it does tell you which method is missing, and
obviously, the object returned by "h" is the one that doesn't support
pos.

> (2) as a naive programmer, with a background in other languages,
> I do not think of pos as method. I think of it as variable.
> Once the system does not find a method, it could check whether
> there is a local variable of same name, and then print out
> an error message like
>    "no method 'pos', no read access to local variable 'pos'".

Absolutely not. Consider:

  class FooReader
    attr_reader :pos
    def initialize(stream)
      @stream = stream
    end
    def open
      @stream.open unless @stream.opened?
      @pos = @stream.pos
    end
    def read(size = 1)
      raise "Stream must be opened" if @pos.nil?
      data = @stream.read(size)
      @pos = @stream.pos
      data
    end
  end

@pos is *never* initialized during the initializer; thus, you know the
stream hasn't even been opened until you call Foo#open.

There is no meaningful correspondence between an instance variable and
its accessor methods. The proper answer here is for you to retrain
your thinking.

-austin
-- 
Austin Ziegler * halostatue / gmail.com
               * Alternate: austin / halostatue.ca
: as of this email, I have [ 6 ] Gmail invitations