Why is Array#to_f not defined?

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? :slight_smile: Surely there is no reason to
NOT have this method for the Array class.

/Fredrik

From: Fredrik [mailto:[email protected]]

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? :slight_smile: 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

Hi,

In message “Re: Why is Array#to_f not defined?”
on Tue, 20 May 2008 17:05:04 +0900, Fredrik [email protected]
writes:

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

#to_f converts the receiver to the float itself.

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

The above code converts the elements in the array. That should be
named differently. Surely this is the reason. Try

ary.map(&:to_f)

if you have newer Ruby.

          matz.

I see… I understand that it is not following the rule of “converting
the receiver to a float”, but I would say that it follows the
“principle of least surprise”.

Of course it is not possible to convert an Array to a Float in any
meaningful way, so to convert each element into a Float is the
behaviour I am expecting.

Well… that’s my thinking anyway.

Fredrik

2008/5/20 Fredrik [email protected]:

I see… I understand that it is not following the rule of “converting
the receiver to a float”, but I would say that it follows the
“principle of least surprise”.

That may be true for you, but it is not for me - and apparently others
as well.

Of course it is not possible to convert an Array to a Float in any
meaningful way, so to convert each element into a Float is the
behaviour I am expecting.

Well, Array#to_f could be defined like this:

class Array
alias :to_i :size
def to_f; to_i.to_f end
end

This is much more meaningful to me than making to_f a conversion of
each element to float. Note, I do not advocate this at all - all I am
saying is that there are more meaningful implementations of to_f than
the one you propose.

Well… that’s my thinking anyway.

Exactly. :slight_smile:

Kind regards

robert

Fredrik wrote:

Did somebody forget to include that? :slight_smile: 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

Of course it is not possible to convert an Array to a Float in any
meaningful way, so to convert each element into a Float is the
behaviour I am expecting.

But an array of floats isn’t a float… (:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs