Forum: Ruby creating a dynamic hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sijo k. (Guest)
on 2008-11-27 10:24
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
Sijo k. (Guest)
on 2008-11-27 10:44
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
Heesob P. (Guest)
on 2008-11-27 11:09
(Received via mailing list)
2008/11/27 Sijo Kg <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.
David A. Black (Guest)
on 2008-11-27 11:37
(Received via mailing list)
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
David A. Black (Guest)
on 2008-11-27 11:40
(Received via mailing list)
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
Sijo k. (Guest)
on 2008-11-28 08:59
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
This topic is locked and can not be replied to.