<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rails, Web 2.0, Data Modeling</title>
	<atom:link href="http://vladzloteanu.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vladzloteanu.wordpress.com</link>
	<description>Vlad Zloteanu&#039;s Technical Blog</description>
	<lastBuildDate>Thu, 01 Dec 2011 13:14:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vladzloteanu.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Rails, Web 2.0, Data Modeling</title>
		<link>http://vladzloteanu.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vladzloteanu.wordpress.com/osd.xml" title="Rails, Web 2.0, Data Modeling" />
	<atom:link rel='hub' href='http://vladzloteanu.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Pass rspec options from rake on command line</title>
		<link>http://vladzloteanu.wordpress.com/2011/03/21/pass-rspec-options-from-rake-on-command-line/</link>
		<comments>http://vladzloteanu.wordpress.com/2011/03/21/pass-rspec-options-from-rake-on-command-line/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 11:23:16 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[ruby-on-rails]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby-on-rais]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=319</guid>
		<description><![CDATA[To pass rspec options from a rake task on command line,  use the env variable SPEC_OPTS. For example, to enable backtrace logging when running all spec tests: rake SPEC_OPTS=-b References: http://rspec.rubyforge.org/rspec/1.1.12/classes/Spec/Rake/SpecTask.html &#160;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=319&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To pass rspec options from a rake task on command line,  use the env variable <strong>SPEC_OPTS</strong>. For example, to enable backtrace logging when running all spec tests:</p>
<pre>rake SPEC_OPTS=-b</pre>
<p>References: <a title="Rake SpecTask" href="http://rspec.rubyforge.org/rspec/1.1.12/classes/Spec/Rake/SpecTask.html" target="_blank">http://rspec.rubyforge.org/rspec/1.1.12/classes/Spec/Rake/SpecTask.html</a></p>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/319/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/319/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/319/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=319&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2011/03/21/pass-rspec-options-from-rake-on-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Git through SSH port forwarding (SSH tunneling)</title>
		<link>http://vladzloteanu.wordpress.com/2010/12/18/git-through-ssh-port-forwarding-ssh-tunneling/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/12/18/git-through-ssh-port-forwarding-ssh-tunneling/#comments</comments>
		<pubDate>Sat, 18 Dec 2010 11:07:03 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[ssh port forwarding]]></category>
		<category><![CDATA[ssh tunelling]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=299</guid>
		<description><![CDATA[Scenario: A.  you are on a firewalled network, from where you can&#8217;t access github (remote port:9418 is not allowed) or B. you want to access a securized git server from home, but your IP is not on the git server&#8217;s whitelist. and, of course, you have ssh access on a server that can do this. &#160; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=299&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Scenario:</h3>
<p>A.  you are on a firewalled network, from where you can&#8217;t access github (remote port:9418 is not allowed)</p>
<p>or</p>
<p>B. you want to access a securized git server from home, but your IP is not on the git server&#8217;s whitelist.</p>
<p>and, of course, you have ssh access on a server that can do this.</p>
<p>&nbsp;</p>
<h3>A. Make a ssh tunnel to the git server</h3>
<blockquote><p>Me &#8211;&gt; ServerWithSSHAccess &#8211;&gt; GitServer</p></blockquote>
<p><span id="more-299"></span>The comand to use is:</p>
<p><code>ssh </code><code>ServerWithSSHAccess </code><code>-L 2000:</code><code>GitServer</code><code>:22 -N </code>,</p>
<p>where <strong>2000</strong> is the <strong>local port</strong> you will connect to, and <strong>22</strong> is the <strong>remote port</strong> used to connect to the git server (if the connection uses git:// instead of default ssh port, change this to <strong>9418</strong>). Then, instead of:</p>
<p><code>git clone GitServer/my_repo</code>,</p>
<p>you must use:</p>
<p><code>git clone localhost:2000/my_repo</code></p>
<p>&nbsp;</p>
<h3>B. Automatically use tunnel to connect to git server</h3>
<p>Furthermore, if the repository is already cloned on your computer, or you want to use the same addresses, you can config your ssh client to automatically pick the tunneled connection when connecting to the git server. In ~/.ssh/config, add:<br />
<code><br />
Host GitServer</code></p>
<p><code>HostName 127.0.0.1</code></p>
<p><code>Port 2000</code></p>
<p>so now you can access your git repo as you were directly connected to it:<br />
<code><br />
git clone GitServer/my_repo</code><br />
<code>git pull</code><br />
<code>git push<br />
</code><br />
etc</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/299/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/299/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/299/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=299&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/12/18/git-through-ssh-port-forwarding-ssh-tunneling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Advice on using Ruby, RVM, Passenger, Rails, Bundler, … in development (via Jérémy Lecour)</title>
		<link>http://vladzloteanu.wordpress.com/2010/08/23/advice-on-using-ruby-rvm-passenger-rails-bundler-%e2%80%a6-indevelopment-via-jeremy-lecour/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/08/23/advice-on-using-ruby-rvm-passenger-rails-bundler-%e2%80%a6-indevelopment-via-jeremy-lecour/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 14:27:09 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/2010/08/23/advice-on-using-ruby-rvm-passenger-rails-bundler-%e2%80%a6-indevelopment-via-jeremy-lecour/</guid>
		<description><![CDATA[This article is the english version of “Conseils pour utiliser Ruby, RVM, Passenger, Rails, Bundler, … en développement”, originaly written in French. If you have a better translation, I’d be happy to use it (full or parts) and credit you. Introduction : Why this advice The dev team I’m working in is composed of 3 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=298&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote cite='http://jeremy.wordpress.com/?p=273' style='overflow:hidden;'><p><a href='http://jeremy.wordpress.com/?p=273' title='Jérémy Lecour'></a> This article is the english version of “Conseils pour utiliser Ruby, RVM, Passenger, Rails, Bundler, … en développement”, originaly written in French. If you have a better translation, I’d be happy to use it (full or parts) and credit you. Introduction : Why this advice The dev team I’m working in is composed of 3 people who use Ruby. Besides me, who’s is spending the most part of my time on Ruby and became “the one who knows” about this stuff, m &#8230; <a href='http://jeremy.wordpress.com/?p=273' title='Jérémy Lecour'>Read More</a></p>
</blockquote>
<p>via <a href='http://jeremy.wordpress.com/?p=273' title='Jérémy Lecour'>Jérémy Lecour</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/298/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/298/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/298/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=298&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/08/23/advice-on-using-ruby-rvm-passenger-rails-bundler-%e2%80%a6-indevelopment-via-jeremy-lecour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby/Rails: Parse large XMLs (SAX parsers, Pull parsers) + example of Pull parser</title>
		<link>http://vladzloteanu.wordpress.com/2010/05/20/rubyrails-parse-large-xmls-sax-parsers-pull-parsers-example-of-nokogiri-pull-parser/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/05/20/rubyrails-parse-large-xmls-sax-parsers-pull-parsers-example-of-nokogiri-pull-parser/#comments</comments>
		<pubDate>Thu, 20 May 2010 14:29:25 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[Data Extraction]]></category>
		<category><![CDATA[ruby-on-rails]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[hapricot]]></category>
		<category><![CDATA[nokogiri]]></category>
		<category><![CDATA[rexml]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sax]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xml parser]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=268</guid>
		<description><![CDATA[When you have to parse huge XML files (hundreds of MB-like), loading the whole XML in memory is not an option. XML parsers The most popular XML parsers can be split in 2 big categories: tree-based (aka DOM parsers) &#8211; that parse the whole XML file and transform it into a huge tree of nodes. event-based [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=268&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When you have to parse huge XML files (hundreds of MB-like), loading the whole XML in memory is not an option.</p>
<p><span id="more-268"></span></p>
<h3>XML parsers</h3>
<p>The most popular XML parsers can be split in 2 big categories:</p>
<ul>
<li>tree-based (aka <a href="http://en.wikipedia.org/wiki/Document_Object_Model" target="_blank">DOM</a> parsers) &#8211; that parse the whole XML file and transform it into a huge tree of nodes.</li>
<li>event-based (<a href="http://en.wikipedia.org/wiki/Simple_API_for_XML" target="_blank">SAX</a> and Pull parsers)</li>
</ul>
<p>Therefore, a DOM parser consumes a lot of memory (since it stores the whole tree in memory) but it is much easier to use. You can do all sorts of  cool stuff on them like xpath selectors, css selectors, converting the xml to a hash, etc. Basically, the vast majority of examples/tutorials of ruby XML are refering to DOM parsers.</p>
<p>However, in the 5% of the situations where the XML file is really big, storing it in memory is not an option.</p>
<h3>Event-based XML parsers</h3>
<p>There are two types of event-based parsers</p>
<ul>
<li><em>Push parsers</em> like SAX, where you react to encoutered tags as you get them. You can find here a <a href="http://www.ioncannon.net/programming/643/fast-xml-parsing-with-ruby/" target="_blank">comparison over the principal ruby SAX parsers.</a></li>
</ul>
<ul>
<li><em>Pull parsers</em>, where you control a &#8220;cursor&#8221; in the XML file that you can move with simple primitives like go up/go down etc.</li>
</ul>
<h3><strong>Declarative XML SAX parsing library</strong></h3>
<p>There is an interesting XML SAX parsing library that automatically parses the file into an object. However, the programmer must declare the structure of the subtree. Check out <a href="http://github.com/pauldix/sax-machine" target="_blank">SaxMachine</a>.</p>
<h3>Example using Nokogiri&#8217;s Pull Parser</h3>
<p>I wanted to implement a method that would iterate over &lt;product&gt; tags. This method would also transform the subtree &lt;product&gt; &#8230; &lt;/product&gt; to a hash. I don&#8217;t have a standard subtree structure. Below is a snipped from my function.</p>
<pre>      def each_offer(input, options={}, &amp;block)
        reader = Nokogiri::XML::Reader input
        tag_name = options[:tag_name] || 'product'

        #search for reader
        until (reader.name == tag_name)
          break unless reader.read
        end

        i            = 1
        level        = 1
        product_node = nil
        elem_name    = nil
        elem_hash = {}

        stack = [[tag_name, reader.attributes]]
        while (reader.read)
          case reader.node_type
            #start element
            when 1
              stack.push([reader.name.to_s, reader.attributes])

            #text element
            when 3, Nokogiri::XML::Node::CDATA_SECTION_NODE
              stack.last[1] = reader.value

            #end element
            when 15
              return if stack.empty?

              elem = stack.pop
              parent = stack.last

              # I finished the node
              if parent.nil?
                yield(elem[1])
                elem = nil
                next
              end

              # else..
              key = elem[0]
              parent_childs = parent[1]
              if parent_childs.has_key?(key)
                unless parent_childs[key].is_a? Array
                  parent_childs[key] = [parent_childs[key]]
                end

                parent_childs[key] &lt;&lt; elem[1]
              else
                parent_childs[key] =  elem[1]
              end
          end
        end
      end</pre>
<h3>References</h3>
<p><strong>ReXML</strong></p>
<p><a href="http://ruby-doc.org/core/classes/REXML/Parsers/SAX2Parser.html">http://ruby-doc.org/core/classes/REXML/Parsers/SAX2Parser.html</a></p>
<p><a href="http://stdlib.rubyonrails.org/libdoc/rexml/rdoc/classes/REXML/Parsers/PullParser.html"><span style="font-weight:normal;">http://stdlib.rubyonrails.org/libdoc/rexml/rdoc/classes/REXML/Parsers/PullParser.html</span></a></p>
<h3>Nokogiri</h3>
<p><a href="http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/SAX/Parser.html">http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/SAX/Parser.html</a></p>
<p><a href="http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/Reader.html">http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/Reader.html</a></p>
<h3>LibXML</h3>
<p><a href="http://libxml.rubyforge.org/rdoc/">http://libxml.rubyforge.org/rdoc/</a></p>
<p><strong>Posts on XML event parser</strong></p>
<p><a href="http://snippets.dzone.com/posts/show/7962" target="_blank">Comparing SAX parsers in Ruby</a></p>
<p><a href="http://stackoverflow.com/questions/2000118/processing-large-xml-file-with-libxml-ruby-chunk-by-chunk" target="_blank">SO question on large XML</a></p>
<p><a href="http://www.drdobbs.com/web-development/184406385#l7" target="_blank">Transforming XML and the ReXML&#8217;s Pull Parser</a></p>
<p><a href="http://surgeworks.com/blog/rails/processing-large-xml-data-files" target="_blank">Processing large XML files (SAX example)</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/268/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=268&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/05/20/rubyrails-parse-large-xmls-sax-parsers-pull-parsers-example-of-nokogiri-pull-parser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby on Rails interview questions &#8211; RoR test from PeopleCentric</title>
		<link>http://vladzloteanu.wordpress.com/2010/05/06/ruby-on-rails-interview-questions-ror-test-from-peoplecentric/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/05/06/ruby-on-rails-interview-questions-ror-test-from-peoplecentric/#comments</comments>
		<pubDate>Thu, 06 May 2010 14:19:35 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[interview questions]]></category>
		<category><![CDATA[online tests]]></category>
		<category><![CDATA[ruby-on-rails]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=282</guid>
		<description><![CDATA[One of the recruiting companies that I have collaborated with in the past decided to publish many of theirs IT tests . You can find, among others, a Ruby on Rails test. Enjoy<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=282&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the recruiting companies that I have collaborated with in the past decided to <a href="http://tools.people-centric.ro/index.php?option=com_otests" target="_blank">publish many of theirs IT tests</a> . You can find, among others, a <a href="http://tools.people-centric.ro/index.php?option=com_otests&amp;view=contact_info&amp;btid=45674&amp;lang=en-GB" target="_blank">Ruby on Rails test</a>. Enjoy <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/282/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/282/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/282/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=282&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/05/06/ruby-on-rails-interview-questions-ror-test-from-peoplecentric/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Free IATA + ICAO airport codes database</title>
		<link>http://vladzloteanu.wordpress.com/2010/05/03/free-iata-icao-airport-codes-database/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/05/03/free-iata-icao-airport-codes-database/#comments</comments>
		<pubDate>Mon, 03 May 2010 10:29:53 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[Data Extraction]]></category>
		<category><![CDATA[airport codes]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[iata airport codes]]></category>
		<category><![CDATA[icao codes]]></category>
		<category><![CDATA[web scraping]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=272</guid>
		<description><![CDATA[Parsed from: Wikipedia. Available on YAML or JSON format. Data example: - :airport_name: Charles de Gaulle International Airport (Roissy Airport) :complete_location: Paris, France :city: Paris :country: "France" :iata_code: CDG :icao_code: LFPG Below is a snippet on how to parse this content using Hpricot. doc = Hpricot(open "http://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_#{letter}") doc.search('table.wikitable tr:not(:first-child):not(.sortbottom)').each do &#124;tr_line&#124; # Iata, Icao, Airport [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=272&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Parsed from: <a href="http://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_A" target="_blank">Wikipedia</a>.</p>
<p>Available on <a href="http://dl.dropbox.com/u/3599489/Data%20Dumps/city_airport_codes.yml" target="_blank">YAML</a> or <a href="http://dl.dropbox.com/u/3599489/Data%20Dumps/city_airport_codes.json" target="_blank">JSON</a> format.</p>
<p>Data example:</p>
<pre>- :airport_name: Charles de Gaulle International Airport (Roissy Airport)
  :complete_location: Paris, France
  :city: Paris
  :country: "France"
  :iata_code: CDG
  :icao_code: LFPG</pre>
<p><span id="more-272"></span></p>
<p>Below is a snippet on how to parse this content using <a href="http://hpricot.com/" target="_blank">Hpricot</a>.</p>
<pre>        doc = Hpricot(open "http://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_#{letter}")

        doc.search('table.wikitable tr:not(:first-child):not(.sortbottom)').each do |tr_line|
          # Iata, Icao, Airport name, Location served
          line_records = (tr_line/'td').map{|el| ActionController::Base.helpers.strip_tags el.inner_html}
          next if line_records[2].blank?
          airport = {:iata_code         =&gt; line_records[0],
                     :icao_code         =&gt; line_records[1],
                     :airport_name      =&gt; line_records[2],
                     :complete_location =&gt; line_records[3],
          }

          location_array = airport[:complete_location].split(',')
          unless location_array.blank?
            airport[:city]    = location_array.first
            airport[:country] = location_array.last
          end

          result &lt;&lt; airport
        end;
      end;</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/272/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=272&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/05/03/free-iata-icao-airport-codes-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Fetch data from heterogenous tables(Rails SQL views, SQL Union)</title>
		<link>http://vladzloteanu.wordpress.com/2010/04/06/fetch-data-from-heterogenous-tablesrails-sql-views-sql-union/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/04/06/fetch-data-from-heterogenous-tablesrails-sql-views-sql-union/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 11:42:03 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[ruby-on-rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[heterogenous data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails_sql_views]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql views]]></category>
		<category><![CDATA[union]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=201</guid>
		<description><![CDATA[Let&#8217;s say you have a &#8216;cars&#8217; table and a &#8216;bikes&#8217; table, and the schema can not be changed. Some fields are the same, and you want to display ( paginate, search etc) a list with products that are both cars and bikes (from both the car and the bike models), sorted by their creation date. This [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=201&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have a &#8216;cars&#8217; table and a &#8216;bikes&#8217; table, and the schema can not be changed. Some fields are the same, and you want to display ( paginate, search etc) a list with products that are both cars and bikes (from both the <em>car</em> and the <em>bike</em> models), sorted by their creation date.</p>
<p>This post is about how this can be accomplished using sql views.</p>
<p><span id="more-201"></span></p>
<div id="_mcePaste">You can create an SQL view mapping the common attributes from both tables. Below is the code for a  MySQL view.</div>
<pre>class CreateProducts &lt; ActiveRecord::Migration</pre>
<pre>def self.up</pre>
<pre>sql = "</pre>
<pre>CREATE ALGORITHM = MERGE VIEW `products` (</pre>
<pre>id,</pre>
<pre>name,</pre>
<pre>class_name,</pre>
<pre>seller_type,</pre>
<pre>seller_id,</pre>
<pre>total_price,</pre>
<pre>created_at,</pre>
<pre>updated_at</pre>
<pre>) AS (</pre>
<pre>SELECT cars.id id, cars.name name,</pre>
<pre>'car' type,</pre>
<pre>cars.seller_type seller_type, cars.seller_id seller_id,</pre>
<pre>cars.total_price,</pre>
<pre>cars.created_at created_at, cars.updated_at updated_at</pre>
<pre>FROM cars</pre>
<pre>)</pre>
<pre>UNION (</pre>
<pre>SELECT bikes.id id, bikes.name name,</pre>
<pre>'bike' type,</pre>
<pre>bikes.seller_type seller_type, bikes.seller_id seller_id,</pre>
<pre>bikes.unit_price,</pre>
<pre>bikes.created_at created_at, bikes.updated_at updated_at</pre>
<pre>FROM bikes</pre>
<pre>)</pre>
<pre>ORDER BY updated_at</pre>
<pre>"</pre>
<pre>execute sql</pre>
<pre>end</pre>
<pre>def self.down</pre>
<pre>execute "DROP VIEW `products`"</pre>
<pre>end</pre>
<pre>end</pre>
<div></div>
<div>After this, you can create the</div>
<pre>class Product &lt; ActiveRecord::Base
end</pre>
<div>model and use it (but only for fetching) just as it would have been a regular model, mapped on a single table. The only (big) exception is that you can not INSERT/UPDATE/DELETE records.</div>
<div></div>
<div>Of course, there are other solutions for this problem of heterogeneous tables, involving creating/merging tables, stored procedures, etc.</div>
<div></div>
<div>More info about the differences between tables, views, stored procedures, you can check <a href="http://databases.aspfaq.com/database/should-i-use-a-view-a-stored-procedure-or-a-user-defined-function.html" target="_blank">http://databases.aspfaq.com/database/should-i-use-a-view-a-stored-procedure-or-a-user-defined-function.html</a> and <a href="http://bytes.com/topic/sql-server/answers/450173-views-vs-stored-procedures-whats-difference" target="_blank">http://bytes.com/topic/sql-server/answers/450173-views-vs-stored-procedures-whats-difference</a>.</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/201/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/201/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/201/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=201&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/04/06/fetch-data-from-heterogenous-tablesrails-sql-views-sql-union/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Phone calling codes (aka International Dialing codes) &#8211; DB Dump / YML format</title>
		<link>http://vladzloteanu.wordpress.com/2010/03/19/phone-calling-codes-aka-international-dialing-codes-db-dump-yml-format/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/03/19/phone-calling-codes-aka-international-dialing-codes-db-dump-yml-format/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 13:55:57 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[Data Extraction]]></category>
		<category><![CDATA[Phone Calling Codes]]></category>
		<category><![CDATA[country calling codes]]></category>
		<category><![CDATA[hpricot]]></category>
		<category><![CDATA[phone codes]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web scraping]]></category>
		<category><![CDATA[wikipedia]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=226</guid>
		<description><![CDATA[Parsed from: http://en.wikipedia.org/wiki/List_of_country_calling_codes with Hpricot: doc = Hpricot(open("http://en.wikipedia.org/wiki/List_of_country_calling_codes")) table = (doc/"table")[4] by_country = {} table.search('/tr').each {&#124;tr&#124; tr0 = tr.search('/td')[0] ; tr1 = tr.search('/td')[1]; by_country[tr0.inner_html.gsub(/&#60;.*&#62;/,  "")] = tr1.search('/a').inner_html.gsub(" ", '').split(/\+/).select{&#124;val&#124; !val.empty?}.map(&#38;:to_i) if tr1  } ; nil Phone calling codes (MySQL dump) Phone calling codes by country (YML format)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=226&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Parsed from: <a title="http://en.wikipedia.org/wiki/List_of_country_calling_codes" href="http://http://en.wikipedia.org/wiki/List_of_country_calling_codes" target="_blank">http://en.wikipedia.org/wiki/List_of_country_calling_codes</a> with Hpricot:</p>
<pre>doc = Hpricot(open("http://en.wikipedia.org/wiki/List_of_country_calling_codes"))</pre>
<pre>table = (doc/"table")[4]
by_country = {}
table.search('/tr').each {|tr| tr0 = tr.search('/td')[0] ; tr1 = tr.search('/td')[1]; by_country[tr0.inner_html.gsub(/&lt;.*&gt;/,  "")] = tr1.search('/a').inner_html.gsub(" ", '').split(/\+/).select{|val| !val.empty?}.map(&amp;:to_i) if tr1  } ; nil</pre>
<p><a href="http://vladzloteanu.files.wordpress.com/2010/03/phone_calling_codes_mysql_dump.doc">Phone calling codes (MySQL dump)</a></p>
<p><a href="http://vladzloteanu.files.wordpress.com/2010/03/phone_calling_codes_by_country_yml.doc">Phone calling codes by country (YML format)</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/226/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=226&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/03/19/phone-calling-codes-aka-international-dialing-codes-db-dump-yml-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Google Maps: Center map and automatic zoom on single address</title>
		<link>http://vladzloteanu.wordpress.com/2010/03/11/google-maps-center-map-automatic-zoom-on-single-address/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/03/11/google-maps-center-map-automatic-zoom-on-single-address/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 16:16:27 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[gmaps]]></category>
		<category><![CDATA[google maps]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=221</guid>
		<description><![CDATA[Snippet to get an automatic zoom level for a geocoded address. map = new GMap2(document.getElementById("map_canvas")); geocoder = new GClientGeocoder(); function showAddress(address) {   geocoder.getLocations(address, function (locations) {   var north = locations.Placemark[0].ExtendedData.LatLonBox.north;   var south = locations.Placemark[0].ExtendedData.LatLonBox.south;   var east = locations.Placemark[0].ExtendedData.LatLonBox.east;   var west = locations.Placemark[0].ExtendedData.LatLonBox.west;   var bounds = new GLatLngBounds(new GLatLng(south, west),   new GLatLng(north, east));   var point = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=221&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">Snippet to get an automatic zoom level for a geocoded address.</div>
<div><span id="more-221"></span></div>
<pre>map = new GMap2(document.getElementById("map_canvas"));
geocoder = new GClientGeocoder();
function showAddress(address) {
  geocoder.getLocations(address, function (locations) {
  var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
  var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
  var east = locations.Placemark[0].ExtendedData.LatLonBox.east;
  var west = locations.Placemark[0].ExtendedData.LatLonBox.west;
  var bounds = new GLatLngBounds(new GLatLng(south, west),
  new GLatLng(north, east));
  var point = new GLatLng(locations.Placemark[0].Point.coordinates[1], locations.Placemark[0].Point.coordinates[0])
  map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
  // Placing a marker in the center of the map
  var marker = new GMarker(point);
  map.addOverlay(marker);
});</pre>
<div id="_mcePaste">}</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/221/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=221&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/03/11/google-maps-center-map-automatic-zoom-on-single-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
		<item>
		<title>Why You Shouldn&#8217;t Use Float for Currency (floating point issues &#8211; explained for Ruby and RoR)</title>
		<link>http://vladzloteanu.wordpress.com/2010/01/11/why-you-shouldnt-use-float-for-currency-floating-point-issues-explained-for-ruby-and-ror/</link>
		<comments>http://vladzloteanu.wordpress.com/2010/01/11/why-you-shouldnt-use-float-for-currency-floating-point-issues-explained-for-ruby-and-ror/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 16:25:45 +0000</pubDate>
		<dc:creator>vladzloteanu</dc:creator>
				<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[Currency]]></category>
		<category><![CDATA[Data Modelling]]></category>
		<category><![CDATA[ruby-on-rails]]></category>
		<category><![CDATA[currency]]></category>
		<category><![CDATA[decimal]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[floating point issues]]></category>
		<category><![CDATA[interview questions]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://vladzloteanu.wordpress.com/?p=192</guid>
		<description><![CDATA[This article is a response to http://vladzloteanu.wordpress.com/2010/01/06/ruby-on-rails-interview-questions-update/ It is a VERY BAD IDEEA to use floating point arithmetics to deal with currency. In most of the programming languages. Basically, because you&#8217;ll end up loosing money . And this (on the great majority of cases) is not desirable . I&#8217;ll show you some magic (that you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=192&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This article is a response to <a href="http://vladzloteanu.wordpress.com/2010/01/06/ruby-on-rails-interview-questions-update/" target="_blank">http://vladzloteanu.wordpress.com/2010/01/06/ruby-on-rails-interview-questions-update/</a></p>
<p>It is a VERY BAD IDEEA to use floating point arithmetics to deal with currency. In most of the programming languages. Basically, because you&#8217;ll end up loosing money <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . And this (on the great majority of cases) is not desirable <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>I&#8217;ll show you some magic (that you may try at home):<br />
<code>~$ ruby --version<br />
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]<br />
~$ irb<br />
irb(main):003:0&gt; (10.12*100).to_i<br />
=&gt;; 1011</code></p>
<p>So, what just happened? <span id="more-192"></span></p>
<p>O.K. Now a little computer science lesson.</p>
<h3>Floating Point (vs Fixed Point)</h3>
<p>From wikipedia:</p>
<blockquote><p>Numbers are in general represented approximately to a fixed number of <a title="Significant figures" href="http://en.wikipedia.org/wiki/Significant_figures">significant digits</a> and scaled using an<a title="Exponentiation" href="http://en.wikipedia.org/wiki/Exponentiation">exponent</a>. The base for the scaling is normally 2, 10 or 16. The typical number that can be represented exactly is of the form:</p>
<dl>
<dd><em>significant digits</em> × <em>base</em><sup><em>exponent</em></sup></dd>
</dl>
<p>The term <em>floating point</em> refers to the fact that the <a title="Radix point" href="http://en.wikipedia.org/wiki/Radix_point">radix point</a> (decimal point, or, more commonly in computers, binary point) can &#8220;float&#8221;; that is, it can be placed anywhere relative to the significant digits of the number. This position is indicated separately in the internal representation, and floating-point representation can thus be thought of as a computer realization of <a title="Scientific notation" href="http://en.wikipedia.org/wiki/Scientific_notation">scientific notation</a>.</p></blockquote>
<h4><span style="font-weight:normal;"><em>Short version: </em></span></h4>
<p>- number is converted to scientific notation, and then coded in: <em>sign bit</em>, <em>exponent</em>, <em>significand </em></p>
<p><span style="font-weight:normal;">- the precision is not fixed</span></p>
<p><span style="font-weight:normal;">- much larger range of representable numbers (much more than a fixed point representation), at the cost of precision</span></p>
<h3>Decimal to Floating Point conversion</h3>
<p>In this representation, a large group of rational numbers <strong>can not be represented in binary with a fixed number of digits</strong>.</p>
<p>For example, if you would like to convert <strong>0.7 in binary format</strong> (the example is from <a href="http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html" target="_blank">http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html</a>) you&#8217;ll end up having an <strong>&#8216;endless&#8217; number</strong> (in fact, a repeating fraction, in binary).</p>
<blockquote>
<table>
<tbody>
<tr>
<td>0.7</td>
<td>× 2 =</td>
<td>1.4</td>
<td>1</td>
<td>Generate 1 and continue with the rest.</td>
</tr>
<tr>
<td>0.4</td>
<td>× 2 =</td>
<td>0.8</td>
<td>0</td>
<td>Generate 0 and continue.</td>
</tr>
<tr>
<td>0.8</td>
<td>× 2 =</td>
<td>1.6</td>
<td>1</td>
<td>Generate 1 and continue with the rest.</td>
</tr>
<tr>
<td>0.6</td>
<td>× 2 =</td>
<td>1.2</td>
<td>1</td>
<td>Generate 1 and continue with the rest.</td>
</tr>
<tr>
<td>0.2</td>
<td>× 2 =</td>
<td>0.4</td>
<td>0</td>
<td>Generate 0 and continue.</td>
</tr>
<tr>
<td>0.4</td>
<td>× 2 =</td>
<td>0.8</td>
<td>0</td>
<td>Generate 0 and continue.</td>
</tr>
<tr>
<td>0.8</td>
<td>× 2 =</td>
<td>1.6</td>
<td>1</td>
<td>Generate 1 and continue with the rest.</td>
</tr>
<tr>
<td>0.6</td>
<td>× 2 =</td>
<td>1.2</td>
<td>1</td>
<td>Generate 1 and continue with the rest.</td>
</tr>
<tr>
<td colspan="4">…</td>
</tr>
</tbody>
</table>
<p>The reason why the process seems to continue endlessly is that it does. The number 7/10, which makes a perfectly reasonable decimal fraction, is a repeating fraction in binary, just as the faction 1/3 is a repeating fraction in decimal. (It repeats in binary as well.) We cannot represent this exactly as a floating point number. The closest we can come in four bits is .1011. Since we already have a leading 1, the best eight-bit number we can make is 1.1011.</p></blockquote>
<p>A good simulator can be fount at <a href="http://babbage.cs.qc.edu/IEEE-754/Decimal.html" target="_blank">http://babbage.cs.qc.edu/IEEE-754/Decimal.html</a></p>
<h3>And now back to our issue..</h3>
<p>Ruby floating point values are stored in binary format.<br />
<strong> 10.12 (decimal) can not be represented exactly in binary</strong>:</p>
<p>&gt;&gt; sprintf(&#8220;%0.50f&#8221;, 10.12)<br />
=&gt; &#8220;10.11999999999999921840299066388979554176330566406250&#8243;</p>
<p>The <strong>default conversion of float to string</strong> (e.g., for output) <strong>rounds</strong><br />
to 6 (or maybe 7) places and truncates trailing zeros:</p>
<p>&gt;&gt; sprintf(&#8220;%0.6f&#8221;, a).sub(/0+\Z/,&#8221;)<br />
=&gt; &#8220;10.12&#8243;</p>
<p><strong>Float#to_i truncates</strong>:</p>
<p>&gt;&gt; sprintf(&#8220;%0.50f&#8221;, 10.12 * 100 )<br />
=&gt; &#8220;1011.99999999999988631316227838397026062011718750000000&#8243;<br />
&gt;&gt; <strong>(10.12 * 100).to_i<br />
=&gt; 1011</strong></p>
<p>A very usefull add/sub/mul/div can be found at <a href="http://www.ecs.umass.edu/ece/koren/arith/simulator/FPMul/" target="_blank">http://www.ecs.umass.edu/ece/koren/arith/simulator/FPMul/</a> . A <strong>more detailed explanation</strong> from there:</p>
<blockquote>
<pre>A    + 1.0100001111010111000010100011110101110000101000111101 *2<sup>3</sup> = 10.12
B    + 1.1001000000000000000000000000000000000000000000000000 *2<sup>6</sup> = 100
<hr size="1" />
A*B  + 1.1111100111111111111111111111111111111111111111111111|011 *2<sup>9</sup>
<hr size="1" />
<h4>Postnormalization Step</h4>

A*B  + 1.1111100111111111111111111111111111111111111111111111|01 *2<sup>9</sup>
<hr size="1" />
<h4>Round to Zero</h4>

A*B  + 1.1111100111111111111111111111111111111111111111111111 *2<sup>9</sup> = 1011.9999999999999</pre>
</blockquote>
<h3><strong>Further references</strong></h3>
<p>Wikipedia provides even more examples of accuracy problems:<a href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladzloteanu.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladzloteanu.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladzloteanu.wordpress.com/192/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladzloteanu.wordpress.com&amp;blog=5902030&amp;post=192&amp;subd=vladzloteanu&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladzloteanu.wordpress.com/2010/01/11/why-you-shouldnt-use-float-for-currency-floating-point-issues-explained-for-ruby-and-ror/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e98c484de260c8fe70784f5a499e0598?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">vladzloteanu</media:title>
		</media:content>
	</item>
	</channel>
</rss>
