Hi --

On Sun, 23 Nov 2003, Charles Comstock wrote:

> Hi,
> There have been a few times in which I have wanted to destructively change the 
> type of a variable, yet as fas I can determine there are only to_i and to_f and 
> not to_i! or to_f!.  It seemed like these would be useful sets of methods to 
> have  in addition to the existing to_i, to_f methods.  In particular this would 
> be nice for situations like:
> 
> ARGV[0].to_i!
> if(ARGV[0] > 5) {...}
> 
> or if you had read in a config file into keyname/value string pairs in a hash
> I commonly find myself using something like:
> 
> config['accuracy'] = config['accuracy'].to_i
> 
> this would be much simpler if it was simply
> 
> config['radix'].to_i!
> 
> While not as useful in expression statements these would be nice and analogous
> to the +=, -= operators that automatically get defined based on definition of
> the matching operator.  It would be nice if the type conversion methods had 
> something that was similar to that.  
> 
> Anyway, seemed useful to me, wondered what everyone else thought,

The built-in ! methods do in-place operations on the same object,
whereas the thing you're looking to do is to reassign the variable to
a different object.  So it's really taking place at a different
logical and/or semantic level.

However, I think there was discussion recently, and possibly in the
past also, of the idea of:

  obj .= method

which would be shorthand for

  obj = obj.method

That's more analogous to the

  obj += other

form, which is shorthand for

  obj = obj + other   # i.e., reassignment to variable, not in-place
                      # modification

(I can't remember if Matz commented on it.)


David

-- 
David A. Black
dblack / wobblini.net