# Convert a Hash into an Array

**URL:** <https://rubytalk.org/t/convert-a-hash-into-an-array/15999>\
**Category:** ruby-talk\
**Created:** [24 January 2005 21:02 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999 "2005-01-24T21:02:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Eustaquio\_Rangel\_de](https://avatars.discourse-cdn.com/v4/letter/_/c4cdca/32.png) [@\_Eustaquio\_Rangel\_de](https://rubytalk.org/u/_Eustaquio_Rangel_de)\
**Post date:** [24 January 2005 21:02 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/1 "2005-01-24T21:02:35Z")

</div>

Hi!

If I have:

h = {"a"=\>"111","b"=\>"222"}  
h.to\_a  
=\> [["a", "111"], ["b", "222"]]

is there a method to convert a Hash into a "plain" Array like

["a","111","b","222"]

Thanks!

- ----------------------------  
Eustáquio "TaQ" Rangel  
[eustaquiorangel@yahoo.com](mailto:eustaquiorangel@yahoo.com)  
[http://beam.to/taq](http://beam.to/taq)  
Usuário GNU/Linux no. 224050

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [24 January 2005 21:05 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/2 "2005-01-24T21:05:11Z")

</div>

Eustaquio Rangel de Oliveira Jr. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> Hi!
> 
> If I have:
> 
> h = {"a"=\>"111","b"=\>"222"}  
> h.to\_a  
> =\> [["a", "111"], ["b", "222"]]
> 
> is there a method to convert a Hash into a "plain" Array like

h.to\_a.flatten will do the trick.

Regards,

&nbsp;&nbsp;&nbsp;Michael

---

<div class="post-metadata">

**Author:** ![\_Eustaquio\_Rangel\_de](https://avatars.discourse-cdn.com/v4/letter/_/c4cdca/32.png) [@\_Eustaquio\_Rangel\_de](https://rubytalk.org/u/_Eustaquio_Rangel_de)\
**Post date:** [24 January 2005 21:14 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/3 "2005-01-24T21:14:50Z")

</div>

Michael Neumann wrote:

Hi Michael!

> h.to\_a.flatten will do the trick.

THANK you very much! 🙂

- ----------------------------  
Eustáquio "TaQ" Rangel  
eustaquiorangel@yahoo.com

> **[The easiest way to create, record and stream live video - Beings](https://beings.com)**
>
> Professional live streaming for marketers. Create, record and stream high-quality live video without being an expert.

Usuário GNU/Linux no. 224050

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [25 January 2005 01:53 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/4 "2005-01-25T01:53:54Z")

</div>

Hi --

> Eustaquio Rangel de Oliveira Jr. wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > Hi!
> > 
> > If I have:
> > 
> > h = {"a"=\>"111","b"=\>"222"}  
> > h.to\_a  
> > =\> [["a", "111"], ["b", "222"]]
> > 
> > is there a method to convert a Hash into a "plain" Array like
> 
> h.to\_a.flatten will do the trick.

However, if any of the hash values are arrays, they'll get flattened  
too:

&nbsp;&nbsp;&nbsp;{ "a" =\> [1,2,3] }.to\_a.flatten # =\> ["a",1,2,3]

unless of course you do:

&nbsp;&nbsp;&nbsp;require 'flattenx' # from RAA  
&nbsp;&nbsp;&nbsp;{ "a" =\> [1,2,3] }.to\_a.flatten\_by(1) # =\> ["a",[1,2,3]]

🙂

David

> **···**
>
> On Tue, 25 Jan 2005, Michael Neumann wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [25 January 2005 08:10 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/5 "2005-01-25T08:10:50Z")

</div>

"David A. Black" \<dblack@wobblini.net\> schrieb im Newsbeitrag  
news:Pine.LNX.4.61.0501241746590.31348@wobblini...

> Hi --
> 
> \> Eustaquio Rangel de Oliveira Jr. wrote:  
> \>\> -----BEGIN PGP SIGNED MESSAGE-----  
> \>\> Hash: SHA1  
> \>\>  
> \>\> Hi!  
> \>\>  
> \>\> If I have:  
> \>\>  
> \>\> h = {"a"=\>"111","b"=\>"222"}  
> \>\> h.to\_a  
> \>\> =\> [["a", "111"], ["b", "222"]]  
> \>\>  
> \>\> is there a method to convert a Hash into a "plain" Array like  
> \>  
> \> h.to\_a.flatten will do the trick.
> 
> However, if any of the hash values are arrays, they'll get flattened  
> too:
> 
> &nbsp;&nbsp;&nbsp;{ "a" =\> [1,2,3] }.to\_a.flatten # =\> ["a",1,2,3]
> 
> unless of course you do:
> 
> &nbsp;&nbsp;&nbsp;require 'flattenx' # from RAA  
> &nbsp;&nbsp;&nbsp;{ "a" =\> [1,2,3] }.to\_a.flatten\_by(1) # =\> ["a",[1,2,3]]
> 
> 🙂

.... or use #inject which also has the advantage of saving the intermediate  
array which is created by Hash#to\_a:

> > h = {"a"=\>"111","b"=\>"222"}

=\> {"a"=\>"111", "b"=\>"222"}

> > h.inject(){|a,(k,v)| a \<\< k \<\< v}

=\> ["a", "111", "b", "222"]

Btw, did I mention that I love #inject? 🙂

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Tue, 25 Jan 2005, Michael Neumann wrote:

---

<div class="post-metadata">

**Author:** ![Richard\_Turner1](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Richard\_Turner1](https://rubytalk.org/u/Richard_Turner1)\
**Post date:** [25 January 2005 09:14 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/6 "2005-01-25T09:14:35Z")

</div>

> \>\> h = {"a"=\>"111","b"=\>"222"}  
> =\> {"a"=\>"111", "b"=\>"222"}  
> \>\> h.inject(){|a,(k,v)| a \<\< k \<\< v}  
> =\> ["a", "111", "b", "222"]

Cool! I was wondering how to convert a hash to an array, dropping the  
keys in the process. I see that inject does the job nicely. Is there  
another way too?

> > h.inject(){|a, (k, v)| a \<\< v}

=\> ["111", "222"]

Cheers,

Richard.

> **···**
>
> On Tue, 2005-01-25 at 17:10 +0900, Robert Klemme wrote:

---

<div class="post-metadata">

**Author:** ![georgesawyer](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@georgesawyer](https://rubytalk.org/u/georgesawyer)\
**Post date:** [28 January 2005 12:20 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/7 "2005-01-28T12:20:56Z")

</div>

(Wistfully) Even though 'inject' is the name in Smalltalk, I feel it were  
better named, 'consolidate'.

> **···**
>
> "Robert Klemme" \<bob.news@gmx.net\> Jan 25, 2005 at 09:09 AM wrote:
> 
> > did I mention that I love #inject? 🙂

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [25 January 2005 09:31 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/8 "2005-01-25T09:31:56Z")

</div>

Richard Turner schrieb:

> Cool! I was wondering how to convert a hash to an array, dropping the  
> keys in the process. I see that inject does the job nicely. Is there  
> another way too?
> 
> > > h.inject(){|a, (k, v)| a \<\< v}
> 
> =\> ["111", "222"]

h = {"a"=\>"111", "b"=\>"222"}  
p h.values # =\> ["111", "222"]

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![linus\_sellberg2](https://avatars.discourse-cdn.com/v4/letter/l/839c29/32.png) [@linus\_sellberg2](https://rubytalk.org/u/linus_sellberg2)\
**Post date:** [28 January 2005 13:15 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/9 "2005-01-28T13:15:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Richard\_Turner1](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Richard\_Turner1](https://rubytalk.org/u/Richard_Turner1)\
**Post date:** [25 January 2005 11:17 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/10 "2005-01-25T11:17:09Z")

</div>

Even better! I thought there must be a method for that but my (very  
quick) search last night didn't find it. Thanks

Richard.

> **···**
>
> On Tue, 2005-01-25 at 18:31 +0900, Pit Capitain wrote:
> 
> > h = {"a"=\>"111", "b"=\>"222"}  
> > p h.values # =\> ["111", "222"]

---

<div class="post-metadata">

**Author:** ![georgesawyer](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@georgesawyer](https://rubytalk.org/u/georgesawyer)\
**Post date:** [28 January 2005 18:00 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/11 "2005-01-28T18:00:53Z")

</div>

incorrect)

More fundamentally, I suggest 'collapse', because the most general concept  
seems of collapsing away one dimension from a multi-dimensional thing.  
Notwithstanding that the method should allow, perhaps, a scalar argument,  
or no arguments. Pushing the concept to all dimensions would be done  
instead first by flatten'ing, then by inject'ing (collapse'ing). Reducing  
the dimensionality by one seems useful: here and there, I seem to have  
heard of it.

I lack some comfort with 'inject', even after time: perhaps because it  
asks us to pass the code for collapsing (an array of) objects, instead of  
asking the objects to know how to collapse themselves: a known  
object-programming principle. Collapse perhaps should be a module method  
of no arguments, mix-inable to any class of objects having the ''  
method.

> **···**
>
> linus sellberg \<sellberg@google.com\> Jan 28, 2005 at 02:12 PM wrote:
> 
> > georgesawyer wrote:
> > 
> > > "Robert Klemme" \<bob.news@gmx.net\> Jan 25, 2005 at 09:09 AM 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

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gabriele\_renzi](https://rubytalk.org/u/gabriele_renzi)\
**Post date:** [28 January 2005 22:45 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/12 "2005-01-28T22:45:51Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Alexander\_Kellett2](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Alexander\_Kellett2](https://rubytalk.org/u/Alexander_Kellett2)\
**Post date:** [28 January 2005 23:09 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/13 "2005-01-28T23:09:23Z")

</div>

the main problem i've had with inject is the naming.  
to me it says 'destructive'. and thusly i'd expect  
the receiver and the argument to be exactly the other  
way around. however swapping them makes no sense as  
it forces the use of another temporary. thusly a  
change in name should be made 🙂

haven't heard any better alternative yet however 😕

Alex

> **···**
>
> On Jan 28, 2005, at 7:00 PM, georgesawyer wrote:
> 
> > I lack some comfort with 'inject', even after time: perhaps because it  
> > asks us to pass the code for collapsing (an array of) objects, instead of  
> > asking the objects to know how to collapse themselves: a known  
> > object-programming principle. Collapse perhaps should be a module method  
> > of no arguments, mix-inable to any class of objects having the ''  
> > method.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [30 January 2005 14:15 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/14 "2005-01-30T14:15:47Z")

</div>

"Alexander Kellett" \<ruby-lists@lypanov.net\> schrieb im Newsbeitrag news:9b411145f76db3c8a017a89ebb9d5611@lypanov.net...

> > I lack some comfort with 'inject', even after time: perhaps because it  
> > asks us to pass the code for collapsing (an array of) objects, instead of  
> > asking the objects to know how to collapse themselves: a known  
> > object-programming principle. Collapse perhaps should be a module method  
> > of no arguments, mix-inable to any class of objects having the ''  
> > method.
> 
> the main problem i've had with inject is the naming.  
> to me it says 'destructive'. and thusly i'd expect  
> the receiver and the argument to be exactly the other  
> way around. however swapping them makes no sense as  
> it forces the use of another temporary. thusly a  
> change in name should be made 🙂
> 
> haven't heard any better alternative yet however 😕

#accum is the only name that fits slightly better than #inject although I'm sure one can come up with misinterpretations of that name also. Anyway: #inject is quite complex so it takes some time to get used to the behavior regardless of how it's named.

\<rant\>  
But: the search for a better name is wasted time IMHO. Things are like they are and changes to base classes generally do far more harm than good (if something is seriously flawed that's another story of course).  
\</rant\>

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Jan 28, 2005, at 7:00 PM, georgesawyer wrote:

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [18 March 2005 01:39 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/15 "2005-03-18T01:39:54Z")

</div>

Robert Klemme wrote:

> "Alexander Kellett" \<ruby-lists@lypanov.net\> schrieb im Newsbeitrag  
> news:9b411145f76db3c8a017a89ebb9d5611@lypanov.net...  
> \> the main problem i've had with inject is the naming.  
> \> to me it says 'destructive'. [...]  
> #accum is the only name that fits slightly better than #inject

although I'm

> sure one can come up with misinterpretations of that name also.

Anyway:

> #inject is quite complex so it takes some time to get used to the

behavior

> regardless of how it's named.
> 
> \<rant\>  
> But: the search for a better name is wasted time IMHO. Things are

like they

> are and changes to base classes generally do far more harm than good

(if

> something is seriously flawed that's another story of course).  
> \</rant\>

I respectfully disagree. I was SURE from the name that it must be some  
function to modify the original array, inserting new entries. It took  
me a long time to understand what it was for.

Obviously we can't ditch the old name (for backwards-compatibility  
reasons) but a better alias that is promoted to new users would be a  
good idea, IMHO.

How about #each\_with\_state for a name? #accumulate (or #accum) is not a  
bad name, but also implies to me that the result will be the  
sum/product/concatenation of values. How would #accum fit for the ri  
example of using #inject to find the longest item in a list, for  
example? Some form of accumulation is one of the more common uses of  
#inject (I gather) but by no means the only use.

Aside - whatever the name, what an excellent, versatile function! For  
example, the linked-to geometric series sum at  
[brpreiss.com](http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt) can be  
written in a more terse and (IMO) more ruby-esque way as simply:

def geometric\_series\_sum( x, n )  
&nbsp;&nbsp;(0..n).inject(0){ |sum,i| sum+x\*\*i }  
end

> **···**
>
> --  
> (-, /\ \/ / /\/

---

<div class="post-metadata">

**Author:** ![Derek\_Wyatt1](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@Derek\_Wyatt1](https://rubytalk.org/u/Derek_Wyatt1)\
**Post date:** [18 March 2005 02:57 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/16 "2005-03-18T02:57:46Z")

</div>

> I respectfully disagree. I was SURE from the name that it must be some  
> function to modify the original array, inserting new entries. It took  
> me a long time to understand what it was for.
> 
> Obviously we can't ditch the old name (for backwards-compatibility  
> reasons) but a better alias that is promoted to new users would be a  
> good idea, IMHO.
> 
> How about #each\_with\_state for a name? #accumulate (or #accum) is not a  
> bad name, but also implies to me that the result will be the  
> sum/product/concatenation of values. How would #accum fit for the ri  
> example of using #inject to find the longest item in a list, for  
> example? Some form of accumulation is one of the more common uses of  
> #inject (I gather) but by no means the only use.

Is not #reduce the name we're looking for? Or is it not faithful enough  
to lisp to rip off the name?

Regs,  
D

---

<div class="post-metadata">

**Author:** ![Mathieu\_Bouchard1](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Mathieu\_Bouchard1](https://rubytalk.org/u/Mathieu_Bouchard1)\
**Post date:** [27 March 2005 02:12 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/17 "2005-03-27T02:12:55Z")

</div>

And even shorter,

def geometric\_series\_sum(x,n) (x\*\*(n+1)-1)/(x-1) end

> **···**
>
> On Fri, 18 Mar 2005, Phrogz wrote:
> 
> > Aside - whatever the name, what an excellent, versatile function! For  
> > example, the linked-to geometric series sum at  
> > [brpreiss.com](http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt) can be  
> > written in a more terse and (IMO) more ruby-esque way as simply:  
> > def geometric\_series\_sum( x, n )  
> > &nbsp;&nbsp;(0..n).inject(0){ |sum,i| sum+x\*\*i }  
> > end
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mathieu Bouchard -=- Montréal QC Canada -=- [http://artengine.ca/matju](http://artengine.ca/matju)

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [18 March 2005 05:41 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/18 "2005-03-18T05:41:27Z")

</div>

Derek Wyatt wrote:

> \> I respectfully disagree. I was SURE from the name that it must be some  
> \> function to modify the original array, inserting new entries. It took  
> \> me a long time to understand what it was for.  
> \>  
> \> Obviously we can't ditch the old name (for backwards-compatibility  
> \> reasons) but a better alias that is promoted to new users would be a  
> \> good idea, IMHO.  
> \>  
> \> How about #each\_with\_state for a name? #accumulate (or #accum) is not a  
> \> bad name, but also implies to me that the result will be the  
> \> sum/product/concatenation of values. How would #accum fit for the ri  
> \> example of using #inject to find the longest item in a list, for  
> \> example? Some form of accumulation is one of the more common uses of  
> \> #inject (I gather) but by no means the only use.
> 
> Is not #reduce the name we're looking for? Or is it not faithful enough  
> to lisp to rip off the name?

I'm convinced there were two main creators for Lisp. One designed the  
language, and the other one (who, incidentally, was an insane monkey),  
came up with the function names.

There's really no perfect name for this method...I'm partial to #fold  
after Haskell & Co. but #accum (without the -ate) may describe it's  
function the best. Then again, this is just wishful thinking.

> Regs,  
> D

E

---

<div class="post-metadata">

**Author:** ![Mathieu\_Bouchard1](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Mathieu\_Bouchard1](https://rubytalk.org/u/Mathieu_Bouchard1)\
**Post date:** [21 March 2005 22:09 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/19 "2005-03-21T22:09:09Z")

</div>

The point with #inject was to be faithful to Smalltalk, which uses that  
name. Again, I am sorry for having recommended #inject.

In GridFlow (my biggest Ruby library), that thing is called #fold. I guess  
I must have taken the name from Haskell. To me it's more  
meaningful-sounding than reduce, but your mileage may vary.

> **···**
>
> On Fri, 18 Mar 2005, Derek Wyatt wrote:
> 
> > \> How about #each\_with\_state for a name? #accumulate (or #accum) is not a  
> > \> bad name, but also implies to me that the result will be the  
> > \> sum/product/concatenation of values. How would #accum fit for the ri  
> > \> example of using #inject to find the longest item in a list, for  
> > \> example? Some form of accumulation is one of the more common uses of  
> > \> #inject (I gather) but by no means the only use.  
> > Is not #reduce the name we're looking for? Or is it not faithful enough  
> > to lisp to rip off the name?
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mathieu Bouchard -=- Montréal QC Canada -=- [http://artengine.ca/matju](http://artengine.ca/matju)

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 March 2005 11:39 UTC](https://rubytalk.org/t/convert-a-hash-into-an-array/15999/20 "2005-03-27T11:39:46Z")

</div>

"Mathieu Bouchard" \<matju@sympatico.ca\> schrieb im Newsbeitrag news:Pine.LNX.4.21.0503262110380.5161-100000@mondrian.artengine.ca...

> > Aside - whatever the name, what an excellent, versatile function! For  
> > example, the linked-to geometric series sum at  
> > [brpreiss.com](http://www.brpreiss.com/books/opus8/programs/pgm02_06.txt) can be  
> > written in a more terse and (IMO) more ruby-esque way as simply:  
> > def geometric\_series\_sum( x, n )  
> > (0..n).inject(0){ |sum,i| sum+x\*\*i }  
> > end
> 
> And even shorter,
> 
> def geometric\_series\_sum(x,n) (x\*\*(n+1)-1)/(x-1) end

LOL ... and more efficient also. Know your math. 🙂

Cheers

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Fri, 18 Mar 2005, Phrogz wrote:

[Next page](https://rubytalk.org/t/convert-a-hash-into-an-array/15999.md?page=2)
