[#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:02315] Question about attribute writers

From: Clemens Hintze <c.hintze@...>
Date: 2000-03-31 21:57:39 UTC
List: ruby-talk #2315
Dave Thomas writes:

...

> 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?

At least I would expect it! It is like answered in your FAQ! How does
Ruby determine what a variable is? It is really stright-forward, IMHO.

   Look if there was an assignment before
      yes => compile a variable access
      no  => compile a method call.

That means, the compiler seems to prefer to compile variable access
over compiling method calls. So

   age / 12

would let the interpreter look whether if has already seen a
assignment to a local variable. As it didn't it compile a method call!
So Fred#age would be called. However

   age = val / 12

looks like an assignment. That means variable access! As Ruby prefer
this, why should the compiler try to compile method call here?

But, however, if it was written as

   self.age = val / 12

then Ruby is not able to think of an variable access here, as this
syntax is invalid at that point! So it will compile a method call to
Fred#age=

Sorry for my confusing explanation. But I hope you got my point
anyway.

> 
> 
> Dave
> 

\cle

-- 
Clemens Hintze  mailto: c.hintze@gmx.net

In This Thread