From: dblack@... Date: 2006-01-09T10:26:50+09:00 Subject: Re: Passing a hash of args Hi -- On Mon, 9 Jan 2006, Jonathan Leighton wrote: > On Mon, 2006-01-09 at 08:01 +0900, James Edward Gray II wrote: >> 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 > > 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 dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black