Tag: RoR

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.

Rails routes: limiting access to application parts from certain domains (hosts)

Sometimes we want to handle different parts of a single application from different domains. A good example of such an approach is when we have a scope containing our API. Wouldn't it be great if it could be served from a different domain than the rest of the application? Of course yes! Approach like this (separating the API from the rest of the app) is used in several popular web applications. For example in Twitter. The app is under twitter.com and the API lies under the api.twitter.com url. When you have a smaller Rails app, probably you maintain the API-part and the user-part in one Rails project. The separate domains approach allows you to easily move to the two different apps, when the time comes.

So, how to limit access to the app parts based on the domain? Let's use constraints. Let's assume, that we have a scope called :api and the scope called :ui. Each scope represents a module in our app:

scope :module => :api do
  # Some resources and additional routes here
  # Api::SomeController...
end

scope :module => :ui do
  # Some resources and additional routes here
  # Ui::SomeController...
end

To make it domain-accessible we just need to wrap it with a block like this one:

constraints(:host => 'my_domain') do
# Routes here
end

where the my_domain is a domain that should be used with our app part. So for the example above, it would look like this:

constraints(:host => 'api.example.com') do
  scope :module => :api do
    # Some resources and additional routes here
    # Api::SomeController...
  end
end

constraints(:host => 'my.app.example.com') do
  scope :module => :ui do
    # Some resources and additional routes here
    # Ui::SomeController...
  end
end

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑