From: dblack@... Date: 2007-02-18T07:36:45+09:00 Subject: Re: Oppinions on RCR for dup on immutable classes Hi -- On Sun, 18 Feb 2007, Gregory Brown wrote: > On 2/17/07, ara.t.howard@noaa.gov wrote: >> one cannot >> know in advance whether it's ok to call something - even if the object >> responds to it. > > With this in mind, what do you guys think of this work around? > >>> [sandal@metta payroll_camping]$ irb >>> class Object >>> def dup? >>> dup >>> rescue TypeError >>> false >>> end >>> end > => nil >>> 3.dup? > => false >>> 3.dup? || 3 > => 3 >>> "foo".dup? > => "foo" false.dup? # => false :-) I'm not sure what I think of it. I'm trying to think of how it would be used, given that it's a ?-method that returns something other than true/false. Would you do this, for example: if (d = x.dup?) I'm not sure that reads very well; it really kind of conceals the fact that d might be an actual dup of x (at least to my eyes). But I'm probably not thinking of all the places it might be used. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)