From: Igor Pirnovar Date: 2012-10-24T02:44:08+09:00 Subject: Re: Name/symbol/object type clash? What is happening here? No doubt, the following code exhibits unacceptable inconsistencies: S = Struct.new(:num) class S; def change_to n; @num = n; end; end s = S.new(3) puts s.num #=> s s.num = 4 puts s.num #=> 4 s.change_to(5) puts s.num #=> 4 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 -- Posted via http://www.ruby-forum.com/.