# Why is Array#to\_f not defined?

**URL:** <https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110>\
**Category:** Ruby\
**Created:** [May 20, 2008, 10:05am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110 "2008-05-20T10:05:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 10:05am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/1 "2008-05-20T10:05:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 10:14am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/2 "2008-05-20T10:14:06Z")

</div>

From: Fredrik [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 10:17am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/3 "2008-05-20T10:17:04Z")

</div>

Hi,

In message “Re: Why is Array#to\_f not defined?”  
on Tue, 20 May 2008 17:05:04 +0900, Fredrik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

```

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 10:31am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/4 "2008-05-20T10:31:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 10:53am UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/5 "2008-05-20T10:53:14Z")

</div>

2008/5/20 Fredrik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/6 "2008-05-20T17:36:37Z")

</div>

Fredrik wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Fredrik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/cab0a1/32.png) [@Fredrik](https://www.ruby-forum.com/u/Fredrik)\
**Post date:** [May 20, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/why-is-array-to-f-not-defined/138110/7 "2008-05-20T15:20:17Z")

</div>

> 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… (:
