creating a dynamic hash

S

Sijo Kg

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
 
S

Sijo Kg

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

sijo
 
H

Heesob Park

2008/11/27 Sijo Kg said:
Hi
I could solve the problem like
hashes = []
for i in 0..elapsedtime.length-1
hashes = {:bevel => 'bevel1',:value => 5}
end
return hashes

You can do it like this

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

HTH,

Park Heesob
 
D

David A. Black

Hi --

2008/11/27 Sijo Kg said:
Hi
I could solve the problem like
hashes = []
for i in 0..elapsedtime.length-1
hashes = {:bevel => 'bevel1',:value => 5}
end
return hashes

You can do it like this

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
D

David A. Black

Hi --

Hi
I could solve the problem like
hashes = []
for i in 0..elapsedtime.length-1
hashes = {: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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
S

Sijo Kg

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 = {:bevel => 'bevel1',:value => 5}
end
return hashes
Here when I commented return hashes it did not work

Sijo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top