Tag: Ruby

Ruby 1.9.3, Gruff and ZeroDivisionError: divided by 0

After updating Ruby from 1.9.2 to 1.9.3, Gruff stopped working. When I was trying to generate charts, it would throw following exception:

ZeroDivisionError: divided by 0
./gems/gruff-0.3.6/lib/gruff/base.rb:1066:in `label'
./gems/gruff-0.3.6/lib/gruff/base.rb:590:in `setup_graph_measurements'
./gems/gruff-0.3.6/lib/gruff/base.rb:532:in `setup_drawing'
./gems/gruff-0.3.6/lib/gruff/base.rb:508:in `draw'
./gems/gruff-0.3.6/lib/gruff/line.rb:53:in `draw'
./gems/gruff-0.3.6/lib/gruff/base.rb:493:in `to_blob'

How to fix this?

If you look into Gruff source code, you will see, that the error occurs in label method:

def label(value)
  label = if ((@spread.to_f % @marker_count.to_f == 0) || 
    !@y_axis_increment.nil?)
    value.to_i.to_s
  elsif @spread > 10.0
    sprintf("%0i", value)
  elsif @spread >= 3.0
    sprintf("%0.2f", value)
  else
    value.to_s
  end

  parts = label.split('.')
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{THOUSAND_SEPARATOR}")
  parts.join('.')
end

As you can see (or not) it happens, that the @marker_count variable is equal 0. When it happens, we try to divide by zero and the ZeroDivisionError exception is raised. To prevent this from happening, we need to patch this method (until the proper fix is released). Lets overwrite this method:

module Gruff
  class Base
    def label(value)
      label = if @marker_count.to_f == 0
        value.to_i.to_s
      elsif ((@spread.to_f % @marker_count.to_f == 0) || 
        !@y_axis_increment.nil?)
        value.to_i.to_s
      elsif @spread > 10.0
        sprintf("%0i", value)
      elsif @spread >= 3.0
        sprintf("%0.2f", value)
      else
        value.to_s
      end

      parts = label.split('.')
      parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{THOUSAND_SEPARATOR}")
      parts.join('.')
    end
  end
end

Thats all. First we check if @marker_count is equal 0 and if so, we return the whole value casted to integer. The rest of method stays the same.

RVM, Ruby 1.9.3-p194, ruby-debugger and “You need to install ruby-debug” on Ubuntu 11.10

Recently I've been upgrading my RVM and Ruby versions. After upgrade I've encountered a problem connected to ruby-debugger. When starting Rails server I always ended with such a message:

You need to install ruby-debug to run the server in debugging mode. 
With gems, use 'gem install ruby-debug'

# Edit: this solutions fixes also this problem:

cannot load such file -- zlib

Ruby-debbug doesn't work to well with Ruby 1.9, so I use following combination in my gemsets:

group :development, :test do
  gem 'linecache19', :git => 'git://github.com/mark-moseley/linecache'
  gem 'ruby-debug-base19x', '~> 0.11.30.pre4'
  gem 'ruby-debug19'
end

Until now, it worked really well, but after the upgrade, the "You need to install ruby-debug" message kept showing again and again (even when the gems from list above were installed). To fix this issue, you need to run following commands:

rm -rf $rvm_path/usr
rvm pkg install zlib

# Posted in 5 lines instead of 1 for better visibility
sudo apt-get install build-essential openssl libreadline6 libreadline6-dev 
sudo apt-get install curl git-core zlib1g zlib1g-dev libssl-dev
sudo apt-get install libyaml-dev  libsqlite3-0 libsqlite3-dev sqlite3
sudo apt-get install libxml2-dev  libxslt-dev autoconf libc6-dev 
sudo apt-get install ncurses-dev automake libtool bison subversion

rvm reinstall 1.9.3-p194

After successful Ruby version reinstall, you should be able to run ruby software with debugger enabled.

# Update
Looks like, you can (as guys suggested in comments) debugger instead of ruby-debugger, however the method above fixes also problem with:

cannot load such file -- zlib

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑