Skip to content

Instantly share code, notes, and snippets.

@kotp
Forked from anatman/dsh.rb
Created July 1, 2010 03:40
Show Gist options
  • Save kotp/459541 to your computer and use it in GitHub Desktop.
Save kotp/459541 to your computer and use it in GitHub Desktop.
def dsh(un, va)
(va.size - 1).downto(1) {|j| va[j] = va[0 .. j].inject(1) { |pr, it| pr * it } }
ar = un.zip va
hs = {}
ar.each {|i| hs[i[0]] = i[1] }
hs
end
=begin
doctest: setup
>> require 'dsh'
=> true
doctest: years
>> years = dsh [:year, :day, :hour, :minute, :second, :millisecond], [ 1, 1, 24, 60, 60, 1000]
=> {:second=>31536000, :minute=>525600, :hour=>8760, :day=>365, :year=>1}
doctest: yrd
>> yds = dsh [:yd, :ft, :in], [1, 3, 12]
=> {:yd=>1, :ft=>3, :in=>36}
=end
@citizen428
Copy link

The main problem I see here are the repeated calls to inject in the downto block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment