From: Todd Benson Date: 2012-10-25T02:39:09+09:00 Subject: Re: Name/symbol/object type clash? What is happening here? On Tue, Oct 23, 2012 at 12:44 PM, Igor Pirnovar wrote: > Due to the problems shown above, perhaps Struct should be taken out of > Ruby paradigm all together. > > However, except in limited initialization circumstances, the use of '@' > variables should be prohibited, and from the point of view of strict > OOP, it is, by convention! The use of 'Struct' should also be > discouraged, and even worse, mixing Struct with class definitions and > usage is ill advised. > > The above problem can be solved if you use accessor methods in place of > '@' variable: > > S = Struct.new(:num) > class S > def show_num; puts "Show:#{self.num}"; end > def change_num(n); self.num = n; end > end > > s = S.new(100) > puts s.num #=> 100 > s.show_num #=> Show:100 > > def s.change_singleton_num(n); self.num = n; end # <<< SOLUTION > > s.num = 1 > puts s.num #=> 1 > s.show_num #=> Show:1 > > s.change_num(2) > puts s.num #=> 2 > s.show_num #=> Show:2 > > s.change_singleton_num(2000) > puts s.num #=> 2000 > s.show_num #=> Show:2000 > > Regards, igor Okay, I see, thanks.