# Passing a hash of args

**URL:** <https://www.ruby-forum.com/t/passing-a-hash-of-args/51896>\
**Category:** Ruby\
**Created:** [January 8, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896 "2006-01-08T23:57:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 8, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/1 "2006-01-08T23:57:34Z")

</div>

Is there a way to pass each item is a hash as a successive argument to a  
function?

ie:

time = {  
:year =\> 2003  
:month =\> 03  
:day =\> 24  
}  
Time.local(time)

(that wouldn’t work but you get the idea)

Cheers

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 12:00am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/2 "2006-01-09T00:00:35Z")

</div>

On Mon, Jan 09, 2006 at 07:55:42AM +0900, Jonathan L. wrote:

> Time.local(time)
> 
> (that wouldn’t work but you get the idea)

Hashes aren’t ordered. Otherwise you could just do  
Time.local(\*time.values).

Time.local(\*[:year, :month, :day].map {|key| time[key]}) is DRYer but  
more  
typing than Time.local(time[:year], time[:month], time[:day])

I look forward to someone else coming up with something clever.

marcel

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 12:03am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/3 "2006-01-09T00:03:36Z")

</div>

Jonathan L. wrote:

> }

Time.local(\*time.values.reverse)

– Daniel

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 12:18am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/4 "2006-01-09T00:18:46Z")

</div>

On Sun, 08 Jan 2006 23:00:42 -0000, Daniel H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > :day =\> 24  
> > }  
> > Time.local(time)
> > 
> > (that wouldn’t work but you get the idea)
> 
> Time.local(\*time.values.reverse)

Pretty sure that’d only work with some hashes, and only if you had less  
than a bucketful of entries?

Cheers,

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 12:36am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/5 "2006-01-09T00:36:55Z")

</div>

On Jan 9, 2006, at 12:18 AM, Ross B. wrote:

> 

Yes, my mistake. (too late to be thinking here…)

James’s suggestion seems to be the best.

– Daniel

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 1:07am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/6 "2006-01-09T01:07:03Z")

</div>

On Mon, 2006-01-09 at 08:01 +0900, James Edward G. II wrote:

> =\> Mon Mar 24 00:00:00 CST 2003  
> Cool. Thanks everyone. I didn’t realise you could do \*myvar when calling  
> a method as well as when defining them. I was basically looking for a  
> way to do the following (which I’ve managed to solve without use of a  
> hash):

class Time  
UNITS = [:year, :month, :day, :hour, :min, :sec, :usec]

def to\_precision(unit)  
args = []  
0.upto(UNITS.index(unit)) { |unit| args \<\<  
method(UNITS[unit]).call }  
Time.local(\*args)  
end  
end

Is that a sensible way to do it?

Cheers

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 12:03am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/7 "2006-01-09T00:03:37Z")

</div>

On Jan 8, 2006, at 4:55 PM, Jonathan L. wrote:

> }  
> Time.local(time)

Well, Hashes are unordered by definition, but as long as we declare  
an order it’s doable:

> > time = {  
> > ?\> :year =\> 2003,  
> > ?\> :month =\> 03,  
> > ?\> :day =\> 24
> 
> > }  
> > =\> {:month=\>3, :year=\>2003, :day=\>24}
> 
> > Time.local(\*time.values\_at(:year, :month, :day))  
> > =\> Mon Mar 24 00:00:00 CST 2003

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/8 "2006-01-09T18:25:49Z")

</div>

On Mon, 2006-01-09 at 10:26 +0900, removed\_email\_address@domain.invalid wrote:

> > Is that a sensible way to do it?
> 
> I would do:
> 
> send(UNITS[unit])
> 
> in preference to the method/call thing. Maybe:
> 
> args = (0…UNITS.index(unit)).map { |unit| send(UNITS[unit]) }

Awesome, thanks. I did feel the method/call bit was slightly ugly.

Cheers

---

<div class="post-metadata">

**Author:** ![johanatan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johanatan](https://www.ruby-forum.com/u/johanatan)\
**Post date:** [January 9, 2006, 2:28am UTC](https://www.ruby-forum.com/t/passing-a-hash-of-args/51896/9 "2006-01-09T02:28:25Z")

</div>

Hi –

On Mon, 9 Jan 2006, Jonathan L. wrote:

> > > > Time.local(\*time.values\_at(:year, :month, :day))  
> > > > def to\_precision(unit)  
> > > > args = []  
> > > > 0.upto(UNITS.index(unit)) { |unit| args \<\<  
> > > > method(UNITS[unit]).call }  
> > > > Time.local(\*args)  
> > > > end  
> > > > end
> 
> Is that a sensible way to do it?

I would do:

send(UNITS[unit])

in preference to the method/call thing. Maybe:

args = (0…UNITS.index(unit)).map { |unit| send(UNITS[unit]) }

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...
