# Convert a Hash into an Array

**URL:** <https://rubytalk.org/t/convert-a-hash-into-an-array/16107>\
**Category:** ruby-talk\
**Created:** [29 January 2005 01:42 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/16107 "2005-01-29T01:42:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_S](https://avatars.discourse-cdn.com/v4/letter/e/3be4f8/32.png) [@E\_S](https://rubytalk.org/u/E_S)\
**Post date:** [29 January 2005 01:42 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/16107/1 "2005-01-29T01:42:33Z")

</div>

> Lähettäjä: gabriele renzi \<rff\_rff@remove-yahoo.it\>  
> Aihe: Re: Convert a Hash into an Array
> 
> linus sellberg ha scritto:  
> \> georgesawyer wrote:  
> \>  
> \>\>\> did I mention that I love #inject? 🙂  
> \>\>  
> \>\> (Wistfully) Even though 'inject' is the name in Smalltalk, I feel it were  
> \>\> better named, 'consolidate'.  
> \>  
> \>  
> \> I don't have anything against #inject, but I could see the case for an  
> \> alias #accumulate, much like SICP's accumulate which afaik is the same  
> \> thing.  
> \>  
> \> (I wouldn't mind #reduce as well, though that would be slightly incorrect)
> 
> #fold would be better imho (even if slightly incorrect)

Well, since #foldr and #foldl are not offered, it may be better to  
call it #accum. On the other hand...

# ...if memory serves...  
module Enumerable  
&nbsp;&nbsp;def foldl(accum, sym)  
&nbsp;&nbsp;&nbsp;&nbsp;self.each do |item| accum = accum.send(sym, item) end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def foldr(accum, sym)  
&nbsp;&nbsp;&nbsp;&nbsp;self.reverse.each do |item| accum = item.send(sym, accum) end  
&nbsp;&nbsp;end  
end

> > [1,2,3,4,5].foldl 0, :-

=\> -15 # (((((0 - 1) - 2) - 3) - 4) - 5)

> > [1,2,3,4,5].foldr 0, :-

=\> 3 # (1 - (2 - (3 - (4 - (5 - 0)))))

> > [true, true, false, true].foldl true, :&

=\> false # ((((true & true) & true) & false) & true)

E
