Destructive type conversion (to_i!, to_f!)

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,

Charles Comstock

Hi –

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

···

On Sun, 23 Nov 2003, Charles Comstock wrote:


David A. Black
dblack@wobblini.net

Hi!

  • Charles Comstock; 2003-11-22, 23:55 UTC:

ARGV[0].to_i!
if(ARGV[0] > 5) {…}

For me the least surprising answer to the question what

a = [0,1,2,3,4,5]
a[10].to_i!
p a

results in given the choices

a) [0, 1, 2, 3, 4, 5]
b) [0, 1, 2, 3, 4, 5, nil, nil, nil, nil, 0]
c) syntax error

is c.

Josef ‘Jupp’ Schugt

···


.-------.
message > 100 kB? / | |
sender = spammer? / | R.I.P.|
text = spam? / | |