Ruby error

R

Ruwan Budha

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[inc*i] = a[inc*i].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:in
`annotate_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:in `draw'
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:in `each'
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:in `draw'
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:in `draw'
from
/usr/lib/ruby/gems/1.8/gems/gruff-0.3.6/lib/gruff/base.rb:487:in `write'
from ./test3.rb:28
 
B

Brian Candler

Ruwan said:
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 = {}
 
R

Ruwan Budha

Brian said:
Ruwan said:
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...
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top