[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why is Array#to_f not defined?

Fredrik

5/20/2008 8:02:00 AM

Ruby (1.8.6) seems to be missing numeric conversion methods for
Arrays :

class Array
def to_f
self.map { |i| i.to_f }
end
end

(and the same for to_i)
Did somebody forget to include that? :) Surely there is no reason to
NOT have this method for the Array class.

/Fredrik
2 Answers

Peña, Botp

5/20/2008 8:14:00 AM

0

From: Fredrik [mailto:fredjoha@gmail.com]=20
# Ruby (1.8.6) seems to be missing numeric conversion methods for
# Arrays :
# class Array
# def to_f
# self.map { |i| i.to_f }
# end
# end
# (and the same for to_i)
# Did somebody forget to include that? :) Surely there is no reason to
# NOT have this method for the Array class.

you assume arrays contain numbers only ??

try it eg on,

%w(this is a test)
[[],[[]],nil,Object.new]


kind regards -botp



Joel VanderWerf

5/20/2008 3:35:00 PM

0

Fredrik wrote:
> Ruby (1.8.6) seems to be missing numeric conversion methods for
> Arrays :
>
> class Array
> def to_f
> self.map { |i| i.to_f }
> end
> end
>
> (and the same for to_i)
> Did somebody forget to include that? :) Surely there is no reason to
> NOT have this method for the Array class.
>
> /Fredrik

Maybe someone expects this:

class Array
def to_f
Float("#{at(0)}.#{at(1)}E#{at(2)}")
end
end

p [2, "0001", 123].to_f # ==> 2.0001e+123

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407