From: dblack@... Date: 2006-03-12T13:46:36+09:00 Subject: Re: from Python to Ruby in 10 seconds Hi -- ` On Sun, 12 Mar 2006, Bill Guindon wrote: > On 3/11/06, dblack@wobblini.net wrote: >> 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. > > Perhaps best said with "when you have no other methods that set/get @x"? Perhaps, though I deliberately didn't qualify it, on the theory that you could have custom-written accessor methods and still refer to something as an "attribute". But that may be different from having multiple things going on with the same variable. I'm not sure.... could you have one or more "changer" methods that weren't quite setter methods, and still have an "attribute"? I guess it does come down to word-play, at a certain point. After all, from Ruby's perspective, it's all just methods. >>> The method names need to passed as symbols: >>> class Foo >>> private :bar >>> end >> >> You can use a string too (private "bar"). > > I sit corrected (he says, hoping to keep his 'standings' in the > 'word-tweaking sweepstakes') > > If nothing else, it serves as a reminder that I should buy your book ;) And think of all those friends and relatives who also might not know that private can take a string! :-) (Disclaimer: I can't remember whether I mention this point in the book or not. But I approve of your logic :-) It's also a reminder that strings can be used in a lot of places where, for whatever reason, it became customary to use symbols. 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