Ruby 1.8.7 String#chars compatibility problem with rails

Recently, I started getting weird "LoadError" with my Rails app on Dreamhost. I had an "Article" model which contained many "Photo" models. When the "Article" controller tried to refer to the photos in its object, I got the following error:
LoadError: Expected {full server path}/photo.rb to define Photo.

I had a photo.rb file which had class Photo defined. I was using attachment_fu plugin to implement photos. After some head scratching, I found that the error was in photo.rb file. Hence photo.rb was not loading correctly, and therefore a load error.

The actual error happened coz the Dreamhost guys had updated Ruby to 1.8.7 which has some incompatibility with Rails 2.0.2 that I was using. When I tried to create a Photo object on the command line, I got this error:
NoMethodError: undefined method '[]' for #<Enumerable:enumerator></code>

Ruby 1.8.7 has String#chars. This returns an Enumerator object but Rails 2.0.2 expects an ActiveSupport::MultiBye::Chars object.

We can avoid this conflict, by putting the following code in the config/initializers/ directory.

  1.  
  2. unless '1.9'.respond_to?(:force_encoding)
  3. String.class_eval do
  4. begin
  5. remove_method :chars
  6. rescue NameError
  7. # OK
  8. end
  9. end
  10. end
  11.  
Bookmark and Share

Comments (3)

Insert comments in an erb file

How does one insert comments in an erb file. The normal http tag to insert comments do not work well when there are embedded ruby statements in the html.

<!-- <b>Hello</b><%= @article.author %> -->

The above statement will not stop the evaluation of article.author. Instead, @article.author will still be evaluated and placed as a comment in the generated html file. Say the author was Balpreet, then we have a html comment:

<!-- <b>Hello</b>Balpreet -->

So, what if we want to just comment stuff out, so that the embedded ruby statements are not evaluated at all. There are basically two ways that I think we can achieve this:

1. Insert block level comments using embedded ruby.
<%
=begin %>
<b>Hello</b><%= @article.author %><%
=end %>

2. Insert conditional
<% if false %>
<b>Hello</b><%= @article.author %>
<% end %>

Bookmark and Share

Leave a Comment

Rfeedparser and Hpricot

RFeedParser is one of the ruby gems to parse rss feeds. Its a translation of Mark Pilgrims's Universal Feed Parser from Python into Ruby. It has a few dependencies and requires you to install a few other gems. The installation instructions are simple and the usage is simple.

One of the dependencies for this was hpricot. Everything was working fine till one day, I updated my hpricot gem and did a cleanup of my rubygems. I started getting an error:

balpreet-pankajs-macbook:digg balpreetpankaj$ ruby bloo.rb
/Library/Ruby/Site/1.8/rubygems.rb:142:in `activate': can't activate hpricot (= 0.6, runtime), already activated hpricot-0.6.164 (Gem::Exception)
from /Library/Ruby/Site/1.8/rubygems.rb:158:in `activate'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `each'
from /Library/Ruby/Site/1.8/rubygems.rb:157:in `activate'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:355:in `new_constants_in'
from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependencies.rb:510:in `require'

After a little digging arnd with rubygems, I figured out that the rfeedparser has a requirement of running with a hpricot gem with version = 0.6. I changed that to >= 0.6 and things started working fine. I was using version 0.9.951 of rfeedparser. If you use rfeedparser and run into a similar problem, this is how you fix it:

1) Update the gemspec for rfeedparser. Go to your gem installation dir. You can find this by doing:
gem env gempath
Then edit the $gemPATH/specifications/rfeedparser0.9.951.gemspec and update all references of hpricot from "=0.6" to ">=0.6".
2). Go to $GEMPATH/gems/rfeedparser-0.9.951/lib/rfeedparser.rb and again update the reference of hpricot.

This should get you rolling with all new versions of hpricot.

Bookmark and Share

Comments (1)

Creating a headline rotator

I was recently reading the book Learning Jquery by Jonathan Chaffer and Karl Swedberg . After reading the chapter on Rotators and Shufflers, which shows how you can create a headline rotator, I decided to implement one for my own website, adevelopedworld.com. adevelopedworld.com is a website which features social entrepreneurs from all around the world. We constantly get updates from the social entrepreneurs that we have featured, and we had to manually update the homepage to show these updates. After implementing the headline rotator, we just keep adding the updates to a text file, and the homepage gets automatically updated. And to top it, the homepage looks all the more dynamic.

The tutorial on how to create the rotator in the book is very complete. I just had to make one change. The tutorial is based on an old version of jQuery which supported XPath expressions. XPath expressions are no longer a part of JQuery. But there are two solutions :
a). Either add the XPath plugin for jQuery, OR
b). Change the XPath expression to a normal jQuery selector expression.

I used the latter.

A live demo of the headline rotator can be found at adevelopedworld.com.

Bookmark and Share

Comments (2)

Crawling the web with Ruby

Recently for one of my projects, I needed to write my own custom crawler to crawl some of my favorite blogs. I am a recent Ruby convert and I am totally in love with the language. So i decided to have the crawler in Ruby. After some Googling around, I found out this very neat crawler, Spider written in Ruby by Mike Burns. You can find the source code here. The code is currently maintained by John Nagro.

The crawler is simple and yet has support for Amazon SQS and memcahed, so that you can run multiple crawler processes at the same time.  There are also some drawbacks:

1. The urls ae just crawled in the order they are discovered ( breadth-first search). There is no way to specify the ordering in which the urls should be searched.

2. There is no HTML parsing on top of the crawled data.

The first drawback was not much of a problem for my purposes, coz I needed to crawl all the web pages of a blog. Order of crawling was not a deal for me. The crawler has a way in which you can specify the urls in a regex form which are to be crawled. This served my purposes.

I needed HTML parsing though. But this was easy. I just used the hpricot gem to do HTML parsing on top of the crawled data:

Here is a sample code, on how to crawl a particular blog using Spider and using hpricot to print out the meta content on each page.

  1.  
  2. require 'rubygems'
  3. require 'spider'
  4. require 'hpricot'
  5.  
  6. Spider.start_at('http://simpliflying.com/') do |s|
  7. # Limit the pages to just this domain.
  8. s.add_url_check do |a_url|
  9. a_url =~ %r{^http://simpliflying.com.*}
  10. end
  11.  
  12. # Handle 404s.
  13. s.on 404 do |a_url, err_code|
  14. puts "URL not found: #{a_url}"
  15. end
  16.  
  17. # Handle 2xx.
  18. s.on :success do |a_url, resp, prior_url|
  19. puts "#{a_url}: #{resp.code}"
  20. doc = Hpricot(resp.body)
  21. (doc/"meta").each do |meta|
  22. puts meta.attributes['content']
  23. end
  24. end
  25.  
  26. # Handle everything.
  27. s.on :any do |a_url, resp|
  28. puts "URL returned anything: #{a_url} with this code #{resp.code}"
  29. end
  30. end
  31.  

A very good Hpricot documentation is available here.

Bookmark and Share

Leave a Comment