On Tuesday, April 6, 2004, 7:42:06 AM, rolo wrote:

> Would this work for you?

> class Example
>     attr_reader :number
>     def initialize
>         @number = '1.0'
>     end
> end

> e = Example.new
puts e.number #=>> will print 1.0

> [rolo] My question is more related to why the concept is denied.


Public access to an object's data has been considered poor coding for
a long time.  Ruby simply says "you cannot (easily) get your hands on
the data, but here is an easy way to define a method to get what
you're after."

If you really want the data directly:

  e.instance_eval "@attribute"

The nice thing about directing all your queries through a method is
that the implementation of that method is liable to change.  If and
when it does, so long as the output remains the same, the client of
the class doesn't have to know.

Cheers,
Gavin