From: Siep Korteling Date: 2008-07-07T08:18:29+09:00 Subject: Re: creating dynamic hashmap Sameera Gayan wrote: > Robert Klemme wrote: >> On 04.07.2008 07:03, Sameera Gayan wrote: >>> any reply will greatly appriciated... >> Can you be more specific about your problem? What kind of user input do >> you expect? How do you want it to be translated into a Hash? etc. >> >> robert > > Hi Robert, > > Thank you for your reply, > > My problem in like this, > > I have a graph to display the crude oil price fluctuation with $. I'm > using "gruff" to generate graphs. This graph should generate for a given > date range. > > In the x-axis of the graph i want to show the date range. > > In 'gruff' it gets x-axis labels as a hash > Ex : g.labels = { 0 => 'Mon', 2 => 'Wed', 4 => 'Fri', 6 => 'Sun' } > > So i want to dynamically create this hash map. Inside a loop. (so i can > loop the dates and create the x-axis depending on the users date range) > > ultimate hash format should be { 0 => 'Mon', 2 => 'Wed', 4 => 'Fri', 6 > => 'Sun'} > > so that i can use it as > > g.labels = hash_lables > > hope i make myself clear. thankx for any reply .. > > cheers > sameera Is this what you need? require 'enumerator' require 'date' print "enter start date: " s = gets print "enter end date: " e = gets start_date = Date.parse(s) end_date = Date.parse(e) dates = (start_date..end_date) @slice_size = 2 @label_key = 0 @hash_labels = {} dates.each_slice(@slice_size) do |date_slice| @hash_labels[@label_key] = date_slice[0].strftime("%a") @label_key += @slice_size end p @hash_labels hth, Siep -- Posted via http://www.ruby-forum.com/.