# 1.9's New Methods

**URL:** <https://www.ruby-forum.com/t/1-9s-new-methods/186997>\
**Category:** Ruby\
**Created:** [April 15, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997 "2010-04-15T13:10:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/1 "2010-04-15T13:10:37Z")

</div>

First a big shout-out to Marc-Andre L. and his backports project. Nice work.

Now I want to ask if others have noticed all the new methods being add  
to 1.9+? I’m quite happy about vast majority of it, but there was at  
least one method I thought pretty peculiar. This Enumerable method:

def flat\_map(&block)  
return to\_enum(:flat\_map) unless block\_given?  
map(&block).flatten(1)  
end unless method\_defined? :flat\_map  
Backports.alias\_method self, :collect\_concat, :flat\_map

I am very curious to know how it was decided that a normal map  
followed by a 1-deep flatten is common enough to warrant its own  
method? Two in fact!

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/2 "2010-04-15T13:31:07Z")

</div>

On Apr 15, 2010, at 4:06 AM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end unless method\_defined? :flat\_map  
> Backports.alias\_method self, :collect\_concat, :flat\_map
> 
> I am very curious to know how it was decided that a normal map  
> followed by a 1-deep flatten is common enough to warrant its own  
> method? Two in fact!

i use Hash[\*collection.map {…}.flatten] all the time.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 1:49pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/3 "2010-04-15T13:49:16Z")

</div>

On Apr 15, 7:30 am, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i use Hash[\*collection.map {…}.flatten] all the time.

I see. But really? So now you will use:

```
    Hash[*collection.flat_map {...}]

```

But that explains why this would never have occurred to me. I use:

```
    collection.map {...}.to_h

```

Thanks.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 1:55pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/4 "2010-04-15T13:55:43Z")

</div>

On Thu, Apr 15, 2010 at 8:30 PM, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Â def flat\_map(&block)

Hash[[1,2,3].map{|i| [i,i\*2]}]

# {1=\>2, 2=\>4, 3=\>6}

RUBY\_DESCRIPTION

# “ruby 1.9.1p378 (2010-01-10 revision 26273) [x86\_64-linux]”

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/5 "2010-04-15T15:56:20Z")

</div>

On Apr 15, 2010, at 1:06 PM, Intransition wrote:

> end unless method\_defined? :flat\_map  
> Backports.alias\_method self, :collect\_concat, :flat\_map
> 
> I am very curious to know how it was decided that a normal map  
> followed by a 1-deep flatten is common enough to warrant its own  
> method? Two in fact!

One advantage of :flat\_map over map(…).flatten(…) is that it can be  
chained in an Enumerator.

Sometimes, this can come in handy.

Regards,  
Florian G.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 16, 2010, 3:24am UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/6 "2010-04-16T03:24:56Z")

</div>

On Apr 15, 2010, at 06:34 , James Edward G. II wrote:

> On Apr 15, 2010, at 6:30 AM, Ryan D. wrote:
> 
> > i use Hash[\*collection.map {…}.flatten] all the time.
> 
> Me too, but it’s needed less in 1.9 I think. Hash[…] now accepts an Array of Arrays.

nice. too bad I don’t use 1.9 at all.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 15, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/7 "2010-04-15T15:34:40Z")

</div>

On Apr 15, 2010, at 6:30 AM, Ryan D. wrote:

> i use Hash[\*collection.map {…}.flatten] all the time.

Me too, but it’s needed less in 1.9 I think. Hash[…] now accepts an  
Array of Arrays.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [April 18, 2010, 12:55am UTC](https://www.ruby-forum.com/t/1-9s-new-methods/186997/8 "2010-04-18T00:55:40Z")

</div>

Intransition wrote:

> end unless method\_defined? :flat\_map  
> Backports.alias\_method self, :collect\_concat, :flat\_map
> 
> I am very curious to know how it was decided that a normal map  
> followed by a 1-deep flatten is common enough to warrant its own  
> method? Two in fact!

#flat\_map is monadic bind (also known as flatMap in Scala, SelectMany  
in .NET and (\>\>=) in Haskell). The other monadic operator that is  
needed to build a monad, is unit (aka return in Haskell), but in an OO  
language, that’s just a factory method, IOW it’s just .new in Ruby.

So, #flat\_map can potentially turn any class that mixes in Enumerable  
into a monad, provided that it also obeys the monad laws, of course.

Whether or not that warrants its own method … well, I have no idea.

jwm
