From: Kent Dahl Date: 2001-08-02T04:58:02+09:00 Subject: [ruby-talk:18990] Re: "Bang" versions of numerical methods on non-immediate values [was: Numbers classes.. Rational number? ] Yukihiro Matsumoto wrote: > immediate values (fixnums and symbols) cannot be modified from method, > so you cannot implement any bang method for fixnums. You need > assignment to alter value in a variable. So for this to work for all numeric objects, we'd need a bang version of an operator short-cut such as *= and friends? That would look bad: a *!= 2 Ok, so chuck that part of the idea in the bin. :-) Would it be difficult to add support to the Ruby interpreter itself to do add! methods "magically" on the variables, as a way to do the assignment behind the users back? Basically writing the *!= nicely? > You want > > | a = b = 1 > | a.add!(1) # a==2 and b==1, due to immediate values > > but since 1 is a fixnum, thus the value of a cannot be changed. > Considering this fact and that fixnums are most common numeric, bang > method for numeric is not a good idea. I mostly wanted to have it in Numeric to be consistent across all numeric objects, and making it transparent for the user, for instance when his variable goes from containing a fixnum to a bignum. But in light of your feedback and the inconsistencies it would introduce, I'll forget about fixnums for now. So, my secondary, trimmed version of the idea: Add add! and friends for Bignum, Float (and Rational?) etc only, forgetting about fixnums. Then I could create one accumulator number before a loop using induced_from: a = Float.induced_from(0) (1...1000).each{ |i| a.add!( 1.0 ) } Basically what I'm looking for, is a way of using Float (and Bignum etc) as close as possible to a double* in C, a wrapper to a value, since the I'm not too fond of the number of babies Float objects make when they do math :-) -- <[ Kent Dahl ]>================<[ http://www.stud.ntnu.no/~kentda/ ]> )____(stud.techn.;ind.�k.data)||(softwareDeveloper.at(Trustix))_( /"Opinions expressed are mine and not those of my Employer, "\ ( "the University, my girlfriend, stray cats, banana fruitflies, " ) \"nor the frontal lobe of my left cerebral hemisphere. "/