# Creating a dynamic hash

**URL:** <https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281>\
**Category:** Ruby\
**Created:** [November 27, 2008, 9:24am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281 "2008-11-27T09:24:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 27, 2008, 9:24am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/1 "2008-11-27T09:24:36Z")

</div>

Hi  
I am trying to draw a graph My question is not related to that  
In the below @tic\_num\_array is an array contains values like  
[“IN1”,“IN2”,“IN3”…] and @tic\_elapsed\_time\_array contains values  
like [10,2,12,…] They are dynamic arrays

chart = Ziya::Charts::Mixed.new( ‘licence’, “status\_chart” )  
chart.add :chart\_types, %w[column line line]  
chart.add :axis\_category\_text, @tic\_num\_array  
chart.add :series, “Ticket” , report(  
“ticket”,@tic\_elapsed\_time\_array )  
chart.add :series, “Warning” , report(  
“warning”,@tic\_elapsed\_time\_array )  
chart.add :theme , “them1”

def report( charttype,elapsedtime )  
case charttype  
when “ticket”  
elapsedtime.each { |etime| Hash.new(“shadow” =\> ‘high’,:bevel  
=\> ‘bevel1’, :value =\> etime.to\_i) }  
when “warning”  
[  
{ :bevel =\> ‘bevel1’, :value =\> 5 },  
{ :bevel =\> ‘bevel1’, :value =\> 5 },  
{ :bevel =\> ‘bevel1’, :value =\> 5 }  
]  
end  
end

```
   The above works fine ..But it is clear the case when "warning" is

```

dynamic So suppose some 20 values theer the above approch is wrong So  
for that I tried the "warning " like  
when “warning”  
(elapsedtime.length-1).times { Hash.new(:bevel =\> ‘bevel1’, :value  
=\> 5)}

```
     Here my I thought the above code for a value elapsedtime.length

```

= 4 generate  
[  
{ :bevel =\> ‘bevel1’, :value =\> 5 },  
{ :bevel =\> ‘bevel1’, :value =\> 5 },  
{ :bevel =\> ‘bevel1’, :value =\> 5 }  
]  
But this does not happen and also the graph now is not working?  
Please help me to solve this

Thanks in advance  
sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 27, 2008, 9:44am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/2 "2008-11-27T09:44:44Z")

</div>

Hi  
I could solve the problem like  
hashes = []  
for i in 0…elapsedtime.length-1  
hashes[i] = {:bevel =\> ‘bevel1’,:value =\> 5}  
end  
return hashes

sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 27, 2008, 10:09am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/3 "2008-11-27T10:09:39Z")

</div>

2008/11/27 Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi  
> I could solve the problem like  
> hashes = []  
> for i in 0…elapsedtime.length-1  
> hashes[i] = {:bevel =\> ‘bevel1’,:value =\> 5}  
> end  
> return hashes

You can do it like this

hashes = [{:bevel =\> ‘bevel1’,:value =\> 5}] \* (elapsedtime.length-1)

HTH,

Park H.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 27, 2008, 10:37am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/4 "2008-11-27T10:37:48Z")

</div>

Hi –

On Thu, 27 Nov 2008, Heesob P. wrote:

> hashes = [{:bevel =\> ‘bevel1’,:value =\> 5}] \* (elapsedtime.length-1)

That’s not the same, because you get the same hash n times, instead of  
n hashes once each.

hashes = [{:x =\> 1, :y =\> 2}] \* 3  
hashes[0].delete(:x)  
p hashes # [{:y=\>2}, {:y=\>2}, {:y=\>2}]

David

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 27, 2008, 10:40am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/5 "2008-11-27T10:40:09Z")

</div>

Hi –

On Thu, 27 Nov 2008, Sijo Kg wrote:

> Hi  
> I could solve the problem like  
> hashes = []  
> for i in 0…elapsedtime.length-1  
> hashes[i] = {:bevel =\> ‘bevel1’,:value =\> 5}  
> end  
> return hashes

Let Ruby do the work 🙂

hashes = Array.new(elapsedtime.length-1) { {:bevel =\> ‘bevel1’,  
:value =\> 5 } }

That gives you an array of the right length, where each element is  
initialized by running the code block, which gives you a new hash each  
time.

David

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [November 28, 2008, 7:59am UTC](https://www.ruby-forum.com/t/creating-a-dynamic-hash/154281/6 "2008-11-28T07:59:47Z")

</div>

Hi  
Thnaks for all the reply…So could you please tell this differnece  
also…I tried both ways  
First

hashes = Array.new(elapsedtime.length) { {:bevel =\> ‘bevel1’,  
:value =\> 5 } }  
Here I did not want the statement  
return hashes

In the second way  
hashes = []  
for i in 0…elapsedtime.length-1  
hashes[i] = {:bevel =\> ‘bevel1’,:value =\> 5}  
end  
return hashes  
Here when I commented return hashes it did not work

Sijo
