smokinguns
smokinguns

Reputation: 709

"nil can't be coerced into Fixnum" Error

All,
I'm banging my head over a piece of code after upgrading to Ruby 1.9.3. It works fine in Ruby 1.8.7. The code greps a process and fetches the "rsize" value for that process, stores it in a file and then plots a graph. But in ruby 1.9.3 I keep getting this error:
ERROR TypeError: nil can't be coerced into Fixnum

SVG/Graph/Graph.rb:375:in `*': nil can't be coerced into Fixnum (TypeError)

Here is the code that monitors the memory usage by Safari process

def begin_memprofile
clear_screen
FileUtils.mkdir_p "#{PATH}"

begin
  loop do
      cmd = "top -l 1 -stats pid,rsize,command |grep Safari | awk '{print $2};'"  
      process_data = `#{cmd}`.split("\n") # array
      arr=process_data[0]
      log "Data: #{arr}"
      if arr =~ /^[0-9]+M[\+\-]?$/  # accepts rsize values like 80M+, 80M-, 80M
          memory= arr.to_s.scan(/\d+/).first.to_i 
      end    
      log "Memory: #{memory}"
      data = [Time.now.to_i, memory]
      write_memory(data) # This function writes data into a csv file
      sleep(INTERVAL)
  end
end
 end

The csv file will have two columns (timestamp, memory). Example value will be like:
1329972936 80
1329972937 50

The above code runs in a loop until user hits CTRL + C. When user does this, I call a function that retrieves the data from csv file and renders a graph

  def render_graph
if !File.exist?(CSV_FILENAME) 
  log "Could not open csv file."
  exit_profiler
end

data = []
fields = []
csv_data = {}


# import data
File.open(CSV_FILENAME, "r").each_line do |line|
  line = line.strip.split(',')
  csv_data[line.first.to_s] = line.last
end
pp "#{csv_data}"



csv_data.each do |row|
  content = row.first
  log "Creating data point: #{content}:: #{row.last.to_i}"
  fields << content
  data << row.last.to_i
end

# strip any nil elements
data.compact!
fields.compact!

        graph = SVG::Graph::Line.new({
  :show_graph_title => true,
  :graph_title => 'Memory Footprint (MB)',
  :height => 800, 
  :width => 1024, 
  :y_title => "RSIZE(MB)",
  :x_title => "TIME",
  :fields => fields
})

graph.add_data(
  :data => data,
  :title => "data"
)

graph.show_y_guidelines = true
graph.min_scale_value = 0

graph.show_x_title = true
graph.show_y_title = true

graph.show_data_points = true
graph.show_data_values = true
graph.show_x_labels = true
graph.rotate_x_labels = false

graph.area_fill = true


# config graph
graph.scale_integers = true
graph.key = false

File.open("#{PATH}/memory-#{TIMESTAMP}.svg", 'w') {|f| 
  f << graph.burn
}    
     end

I'm new to ruby and I'm modifying somebody else's code.I upgraded to ruby 1.9.3 because I wanted sorted hashes. I can't understand why I'm getting this error.

Upvotes: 0

Views: 1353

Answers (3)

Vouters
Vouters

Reputation: 11

SVG::Graph Version 0.6.1 for both Ruby 1.8.x and Ruby 1.9.x can be found at http://vouters.dyndns.org/zip/svg_graph_0.6.1.tar.gz This includes a personal code addition. How to use and install it is described at http://vouters.dyndns.org/tima/HP-UX-OpenView-Ruby-Displaying_System_Metrics_on_Web_browsers.html along with the way I use it.

Yours truly, Philippe

Upvotes: 1

Vouters
Vouters

Reputation: 11

Got it in SVG/Graph/Graph.rb from SVG::Graph V0.6.1. The problem is with methods.include? string

In Ruby V1.9.3 this has to be replaced with self.respond_to?(str).

[root@victor svg_graph_0.6.1]# grep respond_to lib/SVG/Graph/*.rb
lib/SVG/Graph/Graph.rb: set_defaults if self.respond_to?( "set_defaults" )
lib/SVG/Graph/Graph.rb:        calculations if self.respond_to?('calculations')
lib/SVG/Graph/Graph.rb:          self.send( key.to_s+"=", value ) if 
                                       self.respond_to?( key.to_s )

Also the SVG::Graph V0.6.1 install.rb does not work any longer in Ruby 1.9.3. It has to be replaced by a gemspec file.

[root@victor svg_graph_0.6.1]# cat SVG.gemspec
s = Gem::Specification.new do |spec|
        spec.description = <<-EOF
           SVG::Graph V0.6.1 generates HTTP image/svg+xml graphs.
EOF
        spec.author = 'Sean E. Russell'
        spec.email = '[email protected]'
#       spec.rubyforge_project = 'SVG::Graph'
        spec.homepage = 'http://www.germane-software.com/software/SVG/SVG::Graph/'
        spec.name = 'SVG'
        spec.version = '0.6.1'
        spec.summary = 'Ruby based Graphs library.'
        spec.files = Dir['lib/**/*.rb']
#       spec.require_path = '.'
end

Of course from SVG::Graph root directory you have to:

[root@victor svg_graph_0.6.1] mkdir lib
[root@victor svg_graph_0.6.1] cp -R SVG/ lib/

then still from SVG::Graph root directory, you have to

[root@victor svg_graph_0.6.1]# /usr/bin/gem uninstall SVG
[root@victor svg_graph_0.6.1]# /usr/bin/gem build SVG.gemspec
[root@victor svg_graph_0.6.1]# /usr/bin/gem install SVG

I shall make available on my Web site my version of SVG::Graph for Ruby 1.9.3

Yours truly, Philippe

Upvotes: 1

Pedro Rolo
Pedro Rolo

Reputation: 29930

The error means that you have the value nil where you should have a Fixnum. In your place, I would try to use rdebug in order to determine what is the problem with your code.

Upvotes: 0

Related Questions