# Creating dynamic hashmap

**URL:** <https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179>\
**Category:** Ruby\
**Created:** [July 4, 2008, 7:06am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179 "2008-07-04T07:06:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 4, 2008, 7:06am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/1 "2008-07-04T07:06:21Z")

</div>

Hi All,

I have a question about creating a dynamic hash map. I’m using “gruff”  
and in that , the lables parameter should be like this (as in example)

g.labels = { 0 =\> ‘Mon’, 2 =\> ‘Wed’, 4 =\> ‘Fri’, 6 =\> ‘Sun’ }

in my case, above dates are dynamic and user will select the date range.  
So i need to create the hashmap dynamically.

can i use a loop, or … how can i do this,

any reply will greatly appriciated…

thankx guys,

cheers  
sameera

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 4, 2008, 8:45am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/2 "2008-07-04T08:45:37Z")

</div>

On 04.07.2008 07:03, Sameera G. 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

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 4, 2008, 10:11am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/3 "2008-07-04T10:11:00Z")

</div>

Robert K. wrote:

> On 04.07.2008 07:03, Sameera G. 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

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 4, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/4 "2008-07-04T16:59:59Z")

</div>

On 04.07.2008 10:07, Sameera G. wrote:

> Robert K. wrote:
> 
> > On 04.07.2008 07:03, Sameera G. 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.

> g.labels = hash\_lables
> 
> hope i make myself clear. thankx for any reply …

Um, this is pretty basic stuff. Did you actually use Hashes before?  
You will need this to get your key value pairs defined:

[http://www.ruby-doc.org/core/classes/Hash.html#M000161](http://www.ruby-doc.org/core/classes/Hash.html#M000161)

Cheers

robert

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 7, 2008, 4:50am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/5 "2008-07-07T04:50:14Z")

</div>

Hi Siep,

Wow, U saved my day !!! that is what exactly i wanted, Still haven’t try  
it (just now got in to office ;))

I will play around with it and hope it will fix my problem.

thankx a lot again for all Robert and You,

cheers  
sameera

---

<div class="post-metadata">

**Author:** ![sameera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b487fb/32.png) [@sameera](https://www.ruby-forum.com/u/sameera)\
**Post date:** [July 7, 2008, 1:19am UTC](https://www.ruby-forum.com/t/creating-dynamic-hashmap/142179/6 "2008-07-07T01:19:38Z")

</div>

Sameera G. wrote:

> Robert K. wrote:
> 
> > On 04.07.2008 07:03, Sameera G. 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
