From: dblack / superlink.net
Subject: Re: Newbie Q: Data encapsulation with Ruby
Date: Sun, 17 Aug 2003 00:50:56 +0900

Hi David,


(...)

> >  PLEASE!
> >
> >  Dont misundertstood my words as any kind of critism on Ruby !!!
> 
> OK :-)

  :O)
 
> >  I only want to express my current (mis)understanding of that what I
> >  try to understand.
> >
> >  What is the secret behind attr_reader and attr_writer and how do they
> >  support OOP?
> 
> 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.

  Yes...
  But...did I understood so far:
  "Setting" an attribute with "attr_accessor" means it is world
  readable and writeable (or speaking the UNIX-way, it is 
  rw-rw-rw ;) ?

<jokemode>
  I understand fully that I am neither urged by the ruby community to
  give them fully access to my private data nor that there is no other
  destiny in ruby for a variables life than to be of publicc
  access... 
</jokemode>
  
  ...but I only want to try to express what I am currently think of
  haveing understood....(and to struggle with my limited English...)

  X-)


> 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.)

  HU?

  Until this point I thought, "attributes" _are_ instance variables...
  (haveing C++ in mind...)


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

  ...but for me it would break down all "walls" and access controls a
  class would (should?) have over its instance variables...

  Doesn't such a design tend to become more "procedural" (in opposite
  to OOP) than wanted?

  (again, no critism, just a question of a newbie...) 


> 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_*.

  Thank you very much for your help David ! :O)




> David
> 
> -- 
> David Alan Black
> home: dblack / superlink.net
> work: blackdav / shu.edu
> Web:  http://pirate.shu.edu/~blackdav
>             ^^^^^^^^^^^^^^^            


 Oh! an OOP-Url: It declares your "shoe" as "private" ("u" sounds in german
 as the "oe" of "shoe" in English... ;)   ::)))

 Only kidding...
 Keep hacking!
 Meino