[Newbie] Question about instance method scoping/parsing (?)

Discussion in 'Ruby' started by Chris Gernon, Jan 5, 2007.

  1. Chris Gernon

    Chris Gernon Guest

    Can anyone explain to me what's going on here?

    -------------------------------------
    #!/usr/bin/env ruby -w

    class Thingy
    attr_accessor :name

    def test
    puts "self.name: #{self.name}"
    puts "name: #{name}"

    name ||= 'default value'

    puts "self.name: #{self.name}"
    puts "name: #{name}"
    end
    end

    thing1 = Thingy.new
    thing1.name = 'custom value'
    thing1.test
    -------------------------------------
    $ ruby dditest.rb
    self.name: custom value
    name: custom value
    self.name: custom value
    name: default value
    -------------------------------------

    I thought calling a method without a receiver (i.e. name, name=) within
    an instance method would always call that method on the current object.
    Is this not true for setters (i.e. Thingy#name=)?

    Additionally, why is the first instance of name interpreted as a method
    call and the last instance interpreted as (I think) a local variable?

    Thanks,
    Chris

    --
    Posted via http://www.ruby-forum.com/.
     
    Chris Gernon, Jan 5, 2007
    #1
    1. Advertising

  2. Chris Gernon

    Guest

    Hi --

    On Sat, 6 Jan 2007, Chris Gernon wrote:

    > I thought calling a method without a receiver (i.e. name, name=) within
    > an instance method would always call that method on the current object.
    > Is this not true for setters (i.e. Thingy#name=)?


    It is not true for setters. The way it works is that when the parser
    sees:

    bareword = ...

    it defines bareword as a local variable. Therefore, you always need
    an explicit receiver for that kind of method, so Ruby will know it's a
    method.

    There are some interesting side-effects from this. For example:

    if false
    a = 1
    end

    puts a # nil

    Even though "a = 1" is never executed, it's picked up by the parser,
    and a gets defined. That's why "puts a" doesn't raise an "unknown
    method or local variable" error.

    > Additionally, why is the first instance of name interpreted as a method
    > call and the last instance interpreted as (I think) a local variable?


    If both are defined, the local variable takes precedence. You can
    force method interpretation with parentheses:

    name()

    but of course in practice it's better not to reuse the names.


    David

    --
    Q. What is THE Ruby book for Rails developers?
    A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
    Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
    A. Ruby Power and Light, LLC (http://www.rubypal.com)
     
    , Jan 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Barnicle
    Replies:
    10
    Views:
    658
    Bruno Desthuilliers
    Dec 2, 2007
  2. walterbyrd
    Replies:
    16
    Views:
    490
    Steven D'Aprano
    Dec 18, 2008
  3. David Garamond
    Replies:
    5
    Views:
    271
    Ara.T.Howard
    Jun 8, 2004
  4. Raj Singh
    Replies:
    2
    Views:
    212
    Rick DeNatale
    May 29, 2008
  5. Greg Hauptmann
    Replies:
    9
    Views:
    263
    Loren Segal
    Jun 16, 2008
Loading...

Share This Page