From: ara.t.howard@... Date: 2006-12-01T23:28:29+09:00 Subject: Re: time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}} On Fri, 1 Dec 2006, x1 wrote: > What's the best way to make the first item below yield a hash instead > of an array in one line? I'm struggling :( > > #doesnt work > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}} > puts time.type # Array > > #works > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}.first > puts time.type # Hash > puts time['min'] harp:~ > cat a.rb now = Time.now h = %w( min sec ).inject({}){|h,k| h.update k => now.send(k).to_i} p h harp:~ > ruby a.rb {"sec"=>44, "min"=>26} in particular you don't want to call Time.now inside the loop: you'll otherwise sometimes get {"sec"=>0, "min"=>42} when you should have gotten {"sec"=>59, "min"=>41} regards. -a -- if you want others to be happy, practice compassion. if you want to be happy, practice compassion. -- the dalai lama