From: "ara.t.howard" Date: 2008-11-20T00:51:14+09:00 Subject: Re: build hash by iterating On Nov 19, 2008, at 8:31 AM, Jason Lillywhite wrote: > I am building a hash this way: > > h = {} > i = 0 > j = 24 > > while i < 15 do > h[i] = j > i += 1 > j += 6 > end > > => {5=>54, 11=>90, 0=>24, 6=>60, 12=>96, 1=>30, 7=>66, 13=>102, 2=>36, > 8=>72, 14=>108, 3=>42, 9=>78, 4=>48, 10=>84} > > Just what I need, but doesn't seem very Ruby-friendly > > And if I want to change each hash value from fixnum to string, how > would > that be done? > > this is not working: new_hash = h.each_key {|k| h[k].to_s} > > thank you! > -- > Posted via http://www.ruby-forum.com/. > i might do cfp:~ > cat a.rb h = {} and 15.times{|i| h[i] = (24 + (i * 6))} p h.sort p({5=>54, 11=>90, 0=>24, 6=>60, 12=>96, 1=>30, 7=>66, 13=>102, 2=>36, 8=>72, 14=>108, 3=>42, 9=>78, 4=>48, 10=>84}.sort) cfp:~ > ruby a.rb [[0, 24], [1, 30], [2, 36], [3, 42], [4, 48], [5, 54], [6, 60], [7, 66], [8, 72], [9, 78], [10, 84], [11, 90], [12, 96], [13, 102], [14, 108]] [[0, 24], [1, 30], [2, 36], [3, 42], [4, 48], [5, 54], [6, 60], [7, 66], [8, 72], [9, 78], [10, 84], [11, 90], [12, 96], [13, 102], [14, 108]] a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama