From: "David A. Black" Date: 2009-02-09T05:57:55+09:00 Subject: Re: best way to protect class instance variables --1926193751-1591945082-1234126732=:4408 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1591945082-1234126732=:4408" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1591945082-1234126732=:4408 Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT On Mon, 9 Feb 2009, David Masover wrote: > Mike Gold wrote: >> Jes炭s Gabriel y Gal叩n wrote: >> >>> This could save you from accidental changes, but somebody could always >>> do this and trash your frozen array: >>> >>> irb(main):017:0> class Blah >>> irb(main):018:1> @blah = %w{nothing is really safe} >>> irb(main):019:1> end >>> >> >> Data from a closure cannot be overwritten in this way, and could qualify as >> being "really safe". >> > But, you've already provided a method for breaking that anyway -- > define_method. > > So, someone could always do: > > blah = MyClass.blah.dup > blah[0] = 5 > class << MyClass > define_method :blah { blah } > end The blah in { blah } is going to be undefined there, because the class definition block starts a new local scope. However, you can certainly get that effect: def MyClass.blah # some wrong value end and of course: MyClass.blah << 2 (unless it's frozen). I agree with your point about not programming on the assumption that the other programmers who use your code are going to sabotage your classes. David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management! --1926193751-1591945082-1234126732=:4408-- --1926193751-1591945082-1234126732=:4408--