<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Plasmic Peach: Tag Ruby</title>
    <link>http://plasmicpeach.com/articles/tag/ruby?tag=ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The Momo Web Blog</description>
    <item>
      <title>UK Geocoding with Google and Graticule</title>
      <description>&lt;p&gt;At long last, there&amp;#8217;s a good, free geocoding service for the UK.  Finding coordinates based on post code and/or address data has long been a free service in many countries.  But in the UK, the Royal Mail&amp;#8217;s control of post code data &amp;#8211; coupled with the ever-changing nature of that data &amp;#8211; has meant geocoding has been difficult or expensive.  Services like Postcode Anywhere and iShareMaps have provided equivalents to Google&amp;#8217;s geocoder, but for a fee.  That changed in early July 2007, when Google released geocoding for the UK.&lt;/p&gt;&lt;p&gt;If you&amp;#8217;re a Ruby programmer and you&amp;#8217;ve been doing geocoding, it&amp;#8217;s possible you&amp;#8217;ve been using a gem called Graticule.  This gem makes light work of plugging into various geocoding services and performing distance calculations.&lt;/p&gt;


	&lt;p&gt;Graticule includes a couple of classes for UK geocoding, one based on Postcode Anywhere and one based on Local Search Maps.  The first requires a Postcode Anywhere account, which is not free.  The second is free, but in my experience, not very reliable.&lt;/p&gt;


	&lt;p&gt;Neither matter anymore, though, since you can now just use the Google geocoder.  This post explains how to use Graticule to take advantage of Google&amp;#8217;s new UK geocoding service.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m assuming you have a Google Maps &lt;span class="caps"&gt;API&lt;/span&gt; key, and that you have the Graticule gem installed.  (If you don&amp;#8217;t have Graticule, it&amp;#8217;s just &amp;#8220;gem install graticule&amp;#8221;.)&lt;/p&gt;


	&lt;p&gt;In the simplest case, using the UK geocoding service is easy right out of the box.  Just pass the address data to the standard Graticule Google service, and you&amp;#8217;re there:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;require 'rubygems'
require 'graticule'

&amp;gt; g = Graticule.service(:google).new GMAP_API_KEY
#=&amp;gt; #&amp;lt;Graticule::Geocoder::Google:0xb73959f0 @key=&amp;quot;xxxxxxxxxx&amp;quot;,
@url=#&amp;lt;URI::HTTP:0xfdb9cabfe URL:http://maps.google.com/maps/geo&amp;gt;&amp;gt;

&amp;gt; g.locate &amp;quot;NW1 8BX&amp;quot;
#=&amp;gt; #&amp;lt;Graticule::Location:0xb7367064 @longitude=-0.145855,
@region=&amp;quot;England&amp;quot;, @precision=:zip, @latitude=51.542272,
@locality=&amp;quot;London&amp;quot;, @country=&amp;quot;GB&amp;quot;, @street=nil, @postal_code=&amp;quot;NW1 8BX&amp;quot;&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;As you can see, the service correctly found the location in the UK.&lt;/p&gt;


	&lt;p&gt;But sometimes you don&amp;#8217;t have such complete data.  You might be using a partial post code, or a street address and no post code.  In those cases, the service might find a location in the wrong country&amp;#8212;the US, Canada, or Australia, for instance.&lt;/p&gt;


	&lt;p&gt;The easiest way to avoid that problem is to use the UK-specific geocoding service &lt;span class="caps"&gt;URL&lt;/span&gt;.  Graticule&amp;#8217;s Google geocoding class sets its &lt;span class="caps"&gt;URL&lt;/span&gt; to maps.google.com/maps/geo.  What we want is maps.google.co.uk/maps/geo.&lt;/p&gt;


	&lt;p&gt;When you call the method Graticule.service, it returns a geocoder class based on the symbol argument you pass in.  If you pass in :google, it returns the Graticule::Geocoder::Google class.  You could just as easily call Graticule::Geocoder::Google.new.  In either case, the way to get a UK-oriented geocoder is to make a new class that inherits from Graticule::Geocoder::Google, since the UK geocoding service is identical to the international one with the exception of the &lt;span class="caps"&gt;URL&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;In fact, the only method you need to write in the new class is the initialize method, which sets the &lt;span class="caps"&gt;URL&lt;/span&gt;.  Below is the code for the new class.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;graticule&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Graticule&lt;/span&gt;
  &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Geocoder&lt;/span&gt;
    &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;GoogleUk&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Graticule&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Geocoder&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Google&lt;/span&gt;
      &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="keyword"&gt;super&lt;/span&gt;
        &lt;span class="attribute"&gt;@url&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;URI&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;parse&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://maps.google.co.uk/maps/geo&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Put this code in the file &amp;#8220;google_uk.rb&amp;#8221;, in the directory graticule/geocoder somewhere in Ruby&amp;#8217;s include path.  For instance, if you&amp;#8217;re using Rails, you can put it in lib/graticule/geocoder.&lt;/p&gt;


	&lt;p&gt;Now you should be able to use the new UK-oriented geocoder as follows:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&amp;gt; g = Graticule.service(:google_uk).new GMAP_API_KEY
#=&amp;gt; #&amp;lt;Graticule::Geocoder::GoogleUk:0xb734bcb0 @key=&amp;quot;xxxxxxxxxx&amp;quot;,
@url=#&amp;lt;URI::HTTP:0xfdb9a4b3e URL:http://maps.google.co.uk/maps/geo&amp;gt;&amp;gt;

&amp;gt; g.locate &amp;quot;NW1 8BX&amp;quot;
#=&amp;gt; #&amp;lt;Graticule::Location:0xb7367064 @longitude=-0.145855,
@region=&amp;quot;England&amp;quot;, @precision=:zip, @latitude=51.542272,
@locality=&amp;quot;London&amp;quot;, @country=&amp;quot;GB&amp;quot;, @street=nil, @postal_code=&amp;quot;NW1 8BX&amp;quot;&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The UK geocoder works best if you pass it a string containing a complete postcode, or a street address (no apartment number or anything else) plus a partial post code.  Eg, &amp;#8220;AB1 3CD&amp;#8221;, or &amp;#8220;10 Whatever Street, &lt;span class="caps"&gt;AB1&lt;/span&gt;&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;A good way to learn more about the Graticule gem is to explore the &lt;a href="http://rdoc.momoweb.co.uk/graticule"&gt;RDoc documentation&lt;/a&gt; and visit the &lt;a href="http://graticule.com"&gt;Graticule web site&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Wed, 12 Sep 2007 16:15:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8186bf85-754f-494c-871b-df50456a8f12</guid>
      <author>Momo</author>
      <link>http://plasmicpeach.com/articles/2007/09/12/uk-geocoding-with-google-and-graticule</link>
      <category>programming</category>
      <category>Ruby</category>
    </item>
    <item>
      <title>Ruby is better than PHP</title>
      <description>&lt;p&gt;Lots of web developers use &lt;span class="caps"&gt;PHP&lt;/span&gt;.  It&amp;#8217;s an open source programming language and one of the most popular for making web sites.  We don&amp;#8217;t use it, though.  We use Ruby.&lt;/p&gt;


	&lt;p&gt;This is not an arbitrary choice, like preferring purple to green.  We use Ruby because it&amp;#8217;s better.  That&amp;#8217;s what we tell our clients when they ask why we use Ruby.  The next question is usually, &amp;#8220;What makes it better?&amp;#8221;&lt;/p&gt;&lt;p&gt;It&amp;#8217;s pretty hard to answer that question directly, because the reasons are all highly technical.  Instead, we say something such as, &amp;#8220;Ruby is like well-crafted writing.&amp;#8221;  Or, &amp;#8220;It&amp;#8217;s just better designed.&amp;#8221;  Both of them are true.  But how do you know we&amp;#8217;re right, and not just expressing our opinion?  Mightn&amp;#8217;t other people think that &lt;span class="caps"&gt;PHP&lt;/span&gt; is a better language than Ruby?&lt;/p&gt;


	&lt;p&gt;Actually, probably not.  What you&amp;#8217;ll find is that people who&amp;#8217;ve learned Ruby and also &lt;span class="caps"&gt;PHP&lt;/span&gt; nearly all think Ruby is better.  People who know &lt;span class="caps"&gt;PHP&lt;/span&gt; but not Ruby, or &lt;span class="caps"&gt;PHP&lt;/span&gt; and a little Ruby, might tell you that the two languages are equivalent.&lt;/p&gt;


	&lt;p&gt;When talking about any two programming languages used for the same purpose &amp;#8211; call them X and Y &amp;#8211; you&amp;#8217;ll find one of two things. 1: everyone will agree that X and Y are equivalent; or, 2: some people will say X is better than Y, and some people will say X and Y are equivalent.&lt;/p&gt;


	&lt;p&gt;What you almost never find is some people saying &amp;#8220;X is better than Y&amp;#8221;, and some saying &amp;#8220;Y is better than X&amp;#8221;.  Why not?  The reason is that X &lt;em&gt;really is&lt;/em&gt; better than Y &amp;#8211; it&amp;#8217;s a fact &amp;#8211; but you have to know how to program in X pretty well before you realise it.&lt;/p&gt;


	&lt;p&gt;The people who say X and Y are equivalent are Y programmers who can&amp;#8217;t understand the more advanced features of X.  To them, X just looks like another version of Y, with a bunch of other random stuff tacked on.  They don&amp;#8217;t say &amp;#8220;Y is better than X&amp;#8221;, because they can at least see that X can do everything Y can do.  What they can&amp;#8217;t see is what X can do that their language can&amp;#8217;t.&lt;/p&gt;


	&lt;p&gt;So if you find experienced Ruby programmers saying &amp;#8220;Ruby is better than &lt;span class="caps"&gt;PHP&lt;/span&gt;&amp;#8221;, and &lt;span class="caps"&gt;PHP&lt;/span&gt; programmers saying &amp;#8220;Ruby and &lt;span class="caps"&gt;PHP&lt;/span&gt; are equivalent&amp;#8221;, believe the Ruby guys.&lt;/p&gt;</description>
      <pubDate>Sun, 09 Sep 2007 10:18:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c0bcb47b-0087-48a1-849b-ba9b0c846008</guid>
      <author>Momo</author>
      <link>http://plasmicpeach.com/articles/2007/09/09/ruby-is-better-than-php</link>
      <category>programming</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
