From: zuzu Date: 2004-08-29T10:41:22+09:00 Subject: Re: accessors break "no state change outside the object"? On Sun, 29 Aug 2004 10:20:32 +0900, Florian Gross wrote: > zuzu wrote: > > > ruby accessors are merely shortcuts for reading/writing @-scope values > > in an object. > > http://www.ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/accessors.html > > As I understand it Ruby's accessors are just convenient ways of adding > something to the interface of your Object that does modify state > internally. (Without the interface part enforcing that.) > > attr_accessor basically does this: > > class Module > def attr_accessor(name) > define_method(name) do > instance_variable_get(:"@#{name}") > end > define_method(:"#{name}=") do |value| > instance_variable_set(:"@#{name}", value) > end > end > end > > That is, attr_accessor :foo creates two methods foo() and foo=(). You > can also create those manually and they need not be related to an > instance variable at all so I don't think this breaks encapsulation. hmm... ok. > > why create special syntax for initialization / object creation (.new) > > that also isn't specified with the .initialize / .new method? > > I don't see how this is related, but .new / .initialize are *no* special > syntax. i was thinking that commonly accessors were used to modify object-local scope values in ways that seemed like initializing tasks, but i now am rather sure i was mistaken. so nevermind this. > Class#new is defined like this: > > class Class > def new(*args, &block) > result = self.allocate > result.send(:initialize, *args, &block) > return result > end > end > > Class#allocate is special of course, because it needs to construct an > Object. You can't easily do that manually. (It's possible when using > evil-ruby, but there's no real purpose in doing so.) > > So basically I don't see where accessors or Class#new break Ruby's basic > principles. I think Ruby is very consistent here. But then again I might > just not be understanding your objections -- if so, please try to > explain them in more detail. > > > peace, > > -z > > Regards, > Florian Gross thanks, -z