From: Igor Pirnovar Date: 2012-10-27T00:44:02+09:00 Subject: Re: Name/symbol/object type clash? What is happening here? Robert Klemme wrote in post #1081267: > You are repeating that it is broken but you fail to explain what > exactly is broken. That's not a basis for discussions. I thought my statement "I believe, we all can tolerate this glitch" was clear enough. But if you insist to define what is broken, the reply is: "Ruby grammar consistency when it comes to mixing classes with Structs". Every solution that you come up with introduces more complications and weird or even unacceptable run-arounds, like your underscore aliasing. Try using your '_initialize' and then straight 'initialiye' and you'll create two different behaviours both of whom are wrong. One instantiating {{ s = S.new(0) }} with @num to 0, and the other adding 5 twice making @num 10, when using {{ self.num = n + 5 }} idiom rather than {{ @num = n + 5 }}, which Struct does not recognize (this should be fixed, namely, Struct should honour '@ semantics'; resorting to 'self#var' is not sufficient in all circumstances). Struct does not honour Ruby's variable inheritance and class initialization grammar with respect to inheritance, i.e.: all subclasses have a single set of instance variables in the inheritance hierarchy. If the idiom {{ class A < Struct.new(:num); end }} makes Struct a superclass of A, then class A and indeed all its subclasses should inherit @num instance method from Struct. Accessing Struct's @num via {{ self.num }} works only when you are using straight assignment, however if you need to invoke any kind of computation, you have to resort to tricks like aliasing which works only sometimes. S = Struct.new :num do alias _initialize initialize # def _initialize(n) #=> @num==0 def initialize(n) #=> @num==10 super self.num = n + 5 end alias _num= num= def num=(n) self._num= n + 5; end end s = S.new 0 p s.num #=> 10; ## with: '_initialize' #=>0 s.num = 100 p s.num #=> 105 The trouble with Struct is that there is no way to implement initialization of instance variables in base class (ie. in Struct) that require more elaborate initialization skims than straight assignment. You can accomplish this only with regular classes and their inheritance hierarchies! When you have to resort to tricks to accomplish things that are not out of the ordinary, you better avoid those features when working outside of your quick and dirty domain or "research lab", and Struct certainly qualifies for that! Cheers, igor -- Posted via http://www.ruby-forum.com/.