Passing a hash of args

  • Thread starter Jonathan Leighton
  • Start date
J

Jonathan Leighton

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
 
M

Marcel Molina Jr.

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)

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
 
D

Daniel Harple

Jonathan said:
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

Time.local(*time.values.reverse)

-- Daniel
 
J

James Edward Gray II

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)

Well, Hashes are unordered by definition, but as long as we declare
an order it's doable:
?> :year => 2003,
?> :month => 03,
?> :day => 24=> Mon Mar 24 00:00:00 CST 2003

Hope that helps.

James Edward Gray II
 
D

Daniel Harple

On Sun, 08 Jan 2006 23:00:42 -0000, Daniel Harple

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

Cheers,

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

James's suggestion seems to be the best.

-- Daniel
 
J

Jonathan Leighton

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

?> :year => 2003,
?> :month => 03,
?> :day => 24
=> 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
 
D

dblack

Hi --

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

?> :year => 2003,
?> :month => 03,
?> :day => 24
=> {:month=>3, :year=>2003, :day=>24}
=> 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?

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
(e-mail address removed)

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
 
J

Jonathan Leighton

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top