From: dblack@... Date: 2006-03-12T12:49:57+09:00 Subject: Re: from Python to Ruby in 10 seconds Hi -- On Sun, 12 Mar 2006, Bill Guindon wrote: > On 3/11/06, Dave Burt wrote: >> I find Logan's answers accurate and complete, except for one, which is a >> little complicated. Let me add a little to his answer. >> >> John M. Gabriele wrote: >>>> - Python uses _foo, __bar, and __baz__ underscore notation >>>> loosely for private references. Does Ruby have similar >>>> notions of "privacy"? >> >> Logan Capaldo wrote: >>> Method visibility can be controlled via the private, public, and >>> protected key words. You can always get past these of course by use of >>> #instance_eval for instance. >> >> In "foo.bar", bar is always a method, not a variable. What we call >> attributes are methods that behave like attributes. "foo.bar = baz" calls a >> method called bar=. We use attr_accessor, attr_reader and attr_writer to >> create attributes with their own variables. For example, "class Foo; >> attr_accessor :bar; end; foo = Foo.new" > > I found this a bit confusing. Maybe.... > attr_accessor, attr_reader and attr_writer can be used to create > attributes with their own variables. They can also create the > appropriate set/get methods for those (or other) variables. If I can join the word-tweaking sweepstakes: I don't think attributes really have "their own" variables. Give this: class C attr_accessor :x end the methods x and x= have no unique claim to, or ownership of, @x. Maybe one could say: The attr_* methods wrap instance variables in simple, like-named getter and/or setter methods. Given a pair of such methods x and x=, wrapped around @x, objects of the class are said to have an attribute "x". Or something. > The method names need to passed as symbols: > class Foo > private :bar > end You can use a string too (private "bar"). David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black