David Vallner
11/17/2006 2:41:00 PM
Thomas Luedeke wrote:
> I'm reading in a string of numbers, as follows:
>
> " 1 45.3456 "
>
> How do I write out the first non-whitespace element as a number? Ruby
> wants to express as it in ASCII, and the .to_i method doesn't seem to
> work on a substring.
>
Doesn't seem to? Code please.
Also, the following works for me:
irb(main):009:0> " 1 45.3456 ".strip.split(/\s+/).map { |i|
i.to_i
}
=> [1, 45]
(Of course, to_i coerces to integers, so the body of the map block would
have to be more complicated to get floats too.)
David Vallner