[ruby-talk:02323] Re: Question about attribute writers

From: Clemens Hintze <c.hintze@...>
Date: 2000-04-01 00:11:28 UTC
List: ruby-talk #2323
Dave Thomas writes:
> Clemens Hintze <c.hintze@gmx.net> writes:

...

> The reason to make it use the method call is to decouple the use of
> 'age' from it's implementation. For example, say we had

Okay! I agree. But I would not do this! What you mention I would only
do for external clients, but not during internal usage. Internal I
want to deal with that things myself. Perhaps I am practizing bad OO
style here?!? :-/

...

> Just as a matter of interest, though,
> 
>      class Fred
>        private
>        def age=(newAge)
>          @age = newAge
>        end
>        public
>        def setAge(newAge)
>          age = newAge
>        end
>        def initialize
>          @age=99
>        end
>      end
> 
>      f = Fred.new
>      f.setAge(10)
>      p f.inspect
> 
> Shouldn't Ruby throw an error when an assignment accessor is defined
> to be private, as it can never be used.

No, IMO! Two reasons:

1. Ruby does not differ accessors from other methods. Accessors *are*
   normal methods. It happens only that they have the same name as an
   instance variable (with appended '=' for write accessors).

2. If I do:

        def setAge(newAge)
          send "age=", newAge
        end

   then your example works too! :-)))))))

...

> Dave
> 

\cle

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

In This Thread