Monthly Archives: February 2008

The equality methods in ruby confuse me about once a month. So it’s time to permanently embed this information into my brain.

The first confusing part is remembering which object is doing the comparing.


1 == 2
# It makes more sense if you think of it like this
1.==(2)

1 calls it’s method == with the argument 2. I like to think of it as 1 is put in charge of checking if it equals 2. 2 is just along for the ride.The second confusing part is there are several methods that deal with equality but in a slightly different ways.

equal?
Returns true if the object_id’s are equal. Rarely used, and should not be overridden. You can basically forget out it.

a = b = "bob"
a.equal? b # true

a.equal? "bob" # false since the object_id for the literal "bob" is different than will be different

==
The most common equality operator. It is generally used to test the value of objects instead of the object_id’s.

</pre>
string = "bob"

array = [1,2,3]
array == [1,2,3] # true

string == "bob" # true

1 == 1.0 # true

eql?
Just like == but more strict (i.e. returns false for objects are different types.) As far as I can tell, this is only used to compare hash keys. Like the equal? method, you can basically forget about this one.


string = "bob"

string.eql? "bob" # true

1.eql? 1.0 # false since 1 is a Fixnum and 1.0 is a Float

===
Used for case statements. It is full of magic and sprinkled with mystery. This case statement…


case 1

when Numeric then "Number"

when String then "String"

end
# It is equivalent to...
if Numeric === 1 then "Number"

elsif String === 1 then "String"

end

This seems kind of unintuitive since the arguments for === seem to be in the reversed in the case statement. But this technique has more power than greyskull. Check out Regex, Range, and Module to see clever === uses. Also remember a === b may be true, but it’s converse b === a could be false!

Setting up a google sitemap is an easy way to force google to notice your site. A sitemap is just a simple xml file that lists every url you want google to know about.
They are especially useful if…

  • You have dynamic content.
  • Your site is new and google is unaware of it.
  • You use a lot of AJAX or Flash.

You also get the added benefit of seeing where the googlebot looked last, where it encountered errors, and your sites top search keywords.

So it’s helpful, but is it easy to setup? If you’re using Ruby on Rails (or any other ruby based framework) it’s cake!

Step 1: Created a script (RAILS_ROOT/scripts/sitemap)
This script will collect all relevant urls and create a file at RAILS_ROOT/public/sitemap.xml that contains info about each url. For example, let’s pretend we have a site devoted to hippo pictures, our script would look like this…


#!/usr/bin/env ruby

ENV['RAILS_ENV'] ||= "production"

Dir.chdir(File.expand_path(File.dirname(__FILE__) + "/..")) # Change current directory to RAILS_ROOT
require "config/environment" # Start up rails

# These two lines make life super easy... It allows you to call url_for/link_to outside of a controller or view
include ActionController::UrlWriter
default_url_options[:host] = 'www.hippos-are-awesome.com'

filename = "#{RAILS_ROOT}/public/sitemap.xml"

hippo_pics = HippoPic.find(:all) # Such a wonderful collection

File.open(filename, "w") do |file|
  xml = Builder::XmlMarkup.new(:target => file, :indent => 2)

  # This
  xml.instruct!
  xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
    for hippo_pic in hippo_pics
      xml.url do
        xml.loc url_for(:controller => "hippos", :id => hippo_pic.id)
        xml.lastmod hippo_pic.updated_at.xmlschema
        xml.changefreq "weekly"
        xml.priority 0.5
      end
    end
  end
end

For more info about what the lastmod, changefreq and priority mean in the sitemap, google explains it all here. Basically they tell google which urls are more important.

Step 2: Create a daily or weekly cronjob to run the sitemap script
Just switch to the user that runs your ruby apps and add this to its crontab.

20 2 * * * PATH_TO_RAILS_APP/script/sitemap # Runs the sitemap script every morning

Step 3: Let google know about your sitemap
Head over to google’s webmaster tools and follow the instructions on how to point google to your sitemap

That’s it. Some other additional things to consider are

  • gzip your sitemap. Google can read them just fine and you save on bandwidth.
  • If you have more than 50,000 links you need to split your sitemap into several files.
  • Other search engines (like yahoo) can take google style sitemaps too.