[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02309] Question about attribute writers

From: Dave Thomas <Dave@...>
Date: 2000-03-31 18:32:13 UTC
List: ruby-talk #2309
Given:

  class Fred
    def age
      @age
    end
    def age=(val)
      @age = val
    end
    def ageInMonths
      age * 12
    end
    def ageInMonths=(val)
      age = val / 12
    end
    def initialize(age)
      @age = age
    end
  end

  f = Fred.new(10)
  p f.age
  p f.ageInMonths
  f.ageInMonths = 300
  p f.age

I get

  10
  120
  10

The reason is that the assignment

   age = val/12

in Fred#ageInMonths= is creating a local variable 'age', rather than
invoking the age= method.

This seems inconsistent with the fact that

  age / 12

in Fred#ageInMonths is correctly using the attribute reading
function.


Is this the expected behavior?


Dave

In This Thread

Prev Next