dblack / superlink.net wrote:

> attr_reader and attr_writer basically serve the purpose of saving
>
>keystrokes; they don't do anything that you can't do, at slightly
>greater length, by hand.
>
>Specifically, they automatically create some simple get/set methods
>for instance variables.  Once an instance variable has these methods,
>it can be used in an attribute-like way.  (There's actually no
>separate 'attribute' type or category; it's all just methods, with
>attribute-ness being essentially a stylistic component.)
>
>Here's an example:
>
>  class C
>    # define a get method for attribute a
>    def a
>      @a
>    end
>
>    # define a set method for attribute a
>    def a=(x)
>      @a = x
>    end
>  end
>
>  c = C.new
>  c.a = 100      # set the a "attribute" (i.e., call method a= )
>  puts c.a       # get the a attribute (i.e., call method a )
>
>That class can be written like this instead:
>
>  class C
>    attr_reader :a
>    attr_writer :a
>  end
>
>or, even more concisely:
>
>  class C
>    attr_accessor :a      # read and write in one
>  end
>
>The whole attr_* family of methods really just saves some typing.
>
>And of course many combinations and permutations are possible...  I've
>just used some simple examples to show some of the basic operations of
>attr_*.
>  
>

I'd add that in Java, you "shouldn't" (by OO principles), make your data 
public.  You should make
all your data private (or protected) and use accessors to get at that 
data, like:

class Foo
{
    private int bar;

    public int getBar()
    { 
       return bar;
    }

    public void setBar(int x)
    {
       bar = x;
    }
}

All Ruby is doing is automating the otherwise tedious processes you need 
to use in Java.
It's not using any different principles (in fact, it enforces the 
recommended principles
better).

Just an addendum, not a correction.

- Dan

P.S.: It's also the case that people can add methods to your class to 
access private data
you don't want them to. That's just a part of ruby.  I believe the 
#freeze method prevents
that, though.