Why is Array#to_f not defined?

F

Fredrik

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
 
P

Peña, Botp

From: Fredrik [mailto:[email protected]]=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
 
J

Joel VanderWerf

Fredrik said:
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
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top