Destructive type conversion (to_i!, to_f!)

C

Charles Comstock

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
 
J

Josef 'Jupp' SCHUGT

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top