Ruby error

Please help me to figure why ruby is throwing following error…

following is my code.

#!/usr/bin/ruby
require ‘procStartupTimes’
require ‘rubygems’
require ‘gruff’

g = Gruff::Line.new
g.title = “My Graph”

a = [1, 2, 3, 4, 4, 3,2, 3, 1, 5]

g.data(“Apples”, a)

puts a.size

inc = 2

x = Hash.new(a.size/inc)
x[0] = a.first.to_s
x[a.size/inc] = a.last.to_s

for i in 1…((a.size/inc)-1) do
x[inci] = a[inci].to_s
end

puts x.inspect

g.labels = x
g.write(‘my_fruity_graph.png’)


it throws below error…could someone pls tell me what causes this?

10
{5=>“5”, 0=>“1”, 6=>“2”, 2=>“3”, 8=>“1”, 4=>“4”}
/usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/base.rb:1115:in
annotate': can't convert Fixnum into String (TypeError) from /usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/base.rb:1115:inannotate_scaled’
from ./gruff_fix.rb:12:in draw_label' from /usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:80:indraw’
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
each_with_index' from /usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:76:ineach’
from
/usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:76:in
each_with_index' from /usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:76:indraw’
from
/usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:71:in each' from /usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/line.rb:71:indraw’
from
/usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/base.rb:487:in `write’
from ./test3.rb:28

Ruwan B. wrote:

Please help me to figure why ruby is throwing following error…

I know nothing about Gruff, but I do notice this:

x = Hash.new(a.size/inc)

That probably doesn’t do what you think. It says “create a Hash which
returns 5 for any non-existent element” (try puts x[1000] for example)

Hashes don’t have sizes. If you really want a Hash then it should be

x = Hash.new

or just

x = {}

Brian C. wrote:

Ruwan B. wrote:

Please help me to figure why ruby is throwing following error…

I know nothing about Gruff, but I do notice this:

x = Hash.new(a.size/inc)

That probably doesn’t do what you think. It says “create a Hash which
returns 5 for any non-existent element” (try puts x[1000] for example)

Hashes don’t have sizes. If you really want a Hash then it should be

x = Hash.new

or just

x = {}

worked like a charm…thanx so much…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs