<?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/"
	>

<channel>
	<title>Dalibor Novak &#187; Ruby</title>
	<atom:link href="http://dalibornovak.co.uk/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://dalibornovak.co.uk/blog</link>
	<description>Tales from a grumpy developer...</description>
	<lastBuildDate>Sun, 27 Mar 2011 21:02:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Seven Languages in Seven Weeks &#8211; Ruby &#8211; Week 1</title>
		<link>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-ruby-week-1/</link>
		<comments>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-ruby-week-1/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 22:15:29 +0000</pubDate>
		<dc:creator>Dalibor "Bore" Novak</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://dalibornovak.co.uk/blog/?p=29</guid>
		<description><![CDATA[It is the end of the first &#8220;week&#8221; with the book. Due to very busy pre holidays period and holidays it actually took me a month to get through it. I hope things will be less hectic in coming weeks so that I can live up to the book&#8217;s title. So week one with Ruby...<a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-ruby-week-1/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://dalibornovak.co.uk/blog/wp-content/uploads/2011/01/Star-Ruby-The-Delong-Star-Ruby.jpg"><img class="alignleft size-medium wp-image-55" title="Star Ruby (The-Delong-Star-Ruby)" src="http://dalibornovak.co.uk/blog/wp-content/uploads/2011/01/Star-Ruby-The-Delong-Star-Ruby-300x277.jpg" alt="Star Ruby (The-Delong-Star-Ruby)" width="300" height="277" /></a>It is the end of the first &#8220;week&#8221; with the <a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/">book</a>. Due to very busy pre holidays period and holidays it actually took me a month to get through it. I hope things will be less hectic in coming weeks so that I can live up to the book&#8217;s title.</p>
<p>So week one with Ruby then. Everyone interested in software development has almost certainly come across this language. It has captured the interest of disgruntled masses with very productive Rails framework back in 2005. I remember seeing the infamous blog demo videocast and being blown away by the fact that this guy seems to be able to knock up a blog from scratch in about 15 minutes. What I was even more impressed with was how clean and sensible the code he was writing was. I tinkered around with Ruby for a little bit back then, but I got pulled more deeply into the world of Java at that time, as as this was lingua franca in company where I worked and the idea was that it was more important for me to master that than a side language.</p>
<p>So what did I miss out on by doing that?  For one Ruby is a pure Object Oriented programming language which means that everything including literals is an object. It uses strong and dynamic (<a href="http://en.wikipedia.org/wiki/Duck_typing">duck</a>) typing strategies. This allows for polymorphism by functionality rather then by declared types; which gives you more freedom and less typing at the expense of compile time type checking. All the classes in Ruby are <a href="http://rubylearning.com/satishtalim/ruby_open_classes.html">open</a> and can be extended, including classes such as String and NilClass. It supports <a href="http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/">mixins</a> (think interfaces with implementation) as a workaround for multiple inheritance. Provided APIs are rich and frequently provide same functionality via different methods. Ruby and it&#8217;s APIs rely on <a href="http://martinfowler.com/bliki/Closure.html">code blocks</a> (closures, higher order functions &#8211; how ever you want to call them) heavily.  It also provides good <a href="http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox">metaprogramming</a> facilities.  All of this makes it a wonderful and expressive language, but at the same time it gives you all the rope you want to hang yourself.<span id="more-29"></span></p>
<p>Bruce Tate (author) organised each of the chapters into 3 &#8220;days&#8221; per language each followed by self-study a.k.a. &#8220;homework&#8221;.</p>
<p>On day 1 Bruce introduces Ruby and some of it&#8217;s syntax. We learn about the fact that it is purely OO and strongly &#8220;duck&#8221; typed. Even this little knowledge is enough to have so decent fun with Ruby. For instance one of the homework exercises requires you to print your name 10 times, which can be accomplished very elegantly with a following one liner:</p>
<pre class="brush: ruby; title: ; notranslate">10.times { puts 'Dalibor' }</pre>
<p>We can do this as Ruby is purely OO language which means that literal 10 represents an object of Fixnum class. We can lookup API and discover times method that takes a code block; which we use to print the name. This solution is very elegant and also reads very well. With the exception of cryptic puts keyword it could be read by anyone and understood immediately, which is probably not the case with your &#8220;good old&#8221; for loop.</p>
<p>On day 2 both standard collections get introduced Array and Hash. Ruby <a href="http://rubylution.ping.de/articles/2005/12/21/rubys-rich-array-api">rich API</a> strategy gets introduced on the example of Array which can be treated as any of the following: array, stack, queue, set or a linked list. Code blocks get explained (whops! &#8211; I already started using them in day one, but on the other hand we are encouraged by author to explore). Function and class syntax gets explained, which is not very surprising past the fact that due to dynamic typing types don&#8217;t get declared. What is more interesting is how Ruby tackles the multiple inheritance by providing mixins. Mixins get included in a class and extend it with their own behaviour. As they get effectively included in the host class they can access and depend on their functionality. It is explained how <a href="http://www.ruby-doc.org/core/classes/Comparable.html">Comparable</a> and <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Enumerable</a> use this mechanism to provide very rich APIs to classes that include them &#8211; all the host classes have to provide are implementation of &lt;=&gt; operator and each method. An example of a task given on day 2 is implementation of a simple grep tool which I solved like this:</p>
<pre class="brush: ruby; title: ; notranslate">
#!/usr/bin/env ruby

unless ARGV.size == 2
    puts 'Provide name of the file to grep and what to grep for'
    exit
end

counter = 0
File.foreach(ARGV[0]) do |line|
    counter += 1
    puts &quot;#{counter}: #{line}&quot; if line.include? ARGV[1]
end
</pre>
<p>In this example I&#8217;ve used two mechanisms I find interesting. One is String variable substitution &#8211; this is a functionality provided by double quoted strings which allows variables values to be used as part of the string by wrapping them in #{} (see line 11 for example).  Another one is file access using code block. By using foreach method that takes a code block file gets opened and closed for us and executes provided code block for every line in the file. This is quite a lot of functionality from a one method and is also very flexible. I could do something similar in Java, but I would probably have to use an anonymous inner class which would be very verbose.</p>
<p>On day 3 <a href="http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox">metaprogramming</a> hits the stage. Two mechanisms are explained &#8211; metaprogramming via method_missing method. This method gets invoked every time a method that does not exist is invoked on an object. By inspecting the method and arguments passed we can provide implementation for methods that don&#8217;t formally exist on the class. Other (arguably cleaner) mechanism is using modules. This mechanism involves mixing a module into class which then modifies the host by adding new capabilities. This is used by Rails framework to provide ActiveRecord functionality.<br />
Metaprogramming can be demonstrated by following homework assignment. RubyCsv class has ActAsCsv module mixed in. When module is included in a class included method gets invoked. This in turn extends base class (RubyCsv) with ClassMethods module that contains acts_as_csv method. When this method gets invoked it includes InstanceMethods module that reads a csv file and initialises instance variables. It also provides each method that returns instances of CsvRow objects, which uses method_missing to provide &#8220;getters&#8221; for values based on headers.</p>
<pre class="brush: ruby; title: ; notranslate">
#!/usr/bin/env ruby

module ActsAsCsv

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_csv
      include InstanceMethods
    end
  end

  module InstanceMethods

    def read
      @csv_contents = []
      filename = self.class.to_s.downcase + '.txt'
      File.open(filename) do |file|
          @headers = file.gets.chomp.split(', ')

          file.each do |row|
            @csv_contents &lt;&lt; row.chomp.split(', ')
          end
      end
    end

    attr_accessor :headers, :csv_contents

    def each &amp;block
        @csv_contents.each do |line|
            block.call CsvRow.new(@headers, line)
        end
    end

    def initialize
      read
    end

  end

end

class CsvRow

    def initialize headers, row
        @data = {}
        headers.each_index do |index|
            @data[headers[index]] = row[index]
        end
    end

    def method_missing name, *args
        if @data.has_key? name.to_s
            @data[name.to_s]
        end
    end

end

class RubyCsv  # no inheritance! You can mix it in
    include ActsAsCsv
    acts_as_csv
end

m = RubyCsv.new
puts m.headers.inspect
puts m.csv_contents.inspect
m.each {|row| puts &quot;#{row.capital} is capital city of #{row.country}&quot;}

</pre>
<pre class="brush: plain; title: ; notranslate">
country, capital
Slovenia, Ljubljana
United Kingdom, London
</pre>
<p>So if we use provided sample csv file the result we will get will be:</p>
<pre class="brush: plain; title: ; notranslate">
[&quot;country&quot;, &quot;capital&quot;]
[[&quot;Slovenia&quot;, &quot;Ljubljana&quot;], [&quot;United Kingdom&quot;, &quot;London&quot;]]
Ljubljana is capital city of Slovenia
London is capital city of United Kingdom
</pre>
<p>So what are my thoughts on Ruby after my brief encounter with the language. I am impressed with flexibility, expressiveness and productivity of the language. I can see how it can be extended to provide very rich and readable APIs and DSLs. Language feels very malleable due to its metaprogramming support. On the other hand, as much as duck typing helps with keeping the code simple, it does leave me feel a bit exposed to all sorts of type issues, it makes writing IDEs like I&#8217;m used to difficult and surely doesn&#8217;t help with performance. As language allows the programmer to take many liberties with it, I think it warrants quite a lot of discipline and good understanding of the entire codebase of the project you are working on. This makes me think, that working an a project that uses Ruby would probably be pure delight if the team would be knowledgeable and disciplined. I have a sneaky suspicion that it would be a right nightmare to work on a project involving legacy code and a suboptimal team. Especially if there are members that think they know more than they actually do and get entangled in all the rope Ruby will happily provide.</p>
]]></content:encoded>
			<wfw:commentRss>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks-ruby-week-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Seven Languages in Seven Weeks</title>
		<link>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/</link>
		<comments>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 22:34:39 +0000</pubDate>
		<dc:creator>Dalibor "Bore" Novak</dc:creator>
				<category><![CDATA[books]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Io]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://boreplusplus.co.uk/blog/?p=17</guid>
		<description><![CDATA[Lately I have a feeling that Java ecosystem is getting a bit stale. To add to that grass seemed greener on the other side of the fence. Several of my friends that I started my coding career with in J2EE trenches have deserted and moved on to Ruby. None of them ever mentioned regretting the...<a href="http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pragprog.com/titles/btlang/seven-languages-in-seven-weeks"><img class="alignleft size-full wp-image-18" title="Seven Languages in Seven Weeks" src="http://dalibornovak.co.uk/blog/wp-content/uploads/2010/11/sevenLanguages.gif" alt="Book Cover: Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages by Bruce A. Tate" /></a>Lately I have a feeling that Java ecosystem is getting a bit stale. To add to that grass seemed greener on the other side of the fence. Several of my friends that I started my coding career with in J2EE trenches have deserted and moved on to Ruby. None of them ever mentioned regretting the move.</p>
<p>Then new languages that run on JVM started gaining traction and I had a little play with Groovy couple of years ago. I liked the language very much and I grokked why my friends were so happy with Ruby path they&#8217;ve chosen. Sheer expressiveness and productivity provided were impressive. I tried to push for Groovy to be used at work but it never got beyond utilities and scripts. I once read that someone said that the problem Groovy has, is that it is not called Business Logic Expression Language. It seems that sentiment rings quite true.</p>
<p>Anyway as I mentioned earlier Java no longer feels quite like a nice new comfy shirt. I am feeling that it&#8217;s constraining me and due to my brushes with alternative, there is always that little question in the back of my head: &#8220;Am I using the proverbial golden hammer?&#8221;</p>
<p>Recently I started looking over the fence again and I am seeing that there seems to be more choice than ever. One of the languages that sparked my interest was Scala. I checked out some of the websites devoted to it and found that community seems quite vibrant and that the language itself is very interesting indeed. Also the fact that it allows and encourages functional programming sparked my interest even further. I am quite familiar with object orientation and have merely brushed with functional programming at uni. The prospect of getting my head around something new and the promises that functional programming paradigm brings (especially regarding concurrency) got me quite excited.</p>
<p>As I almost decided to devote more time to Scala I have attended <a href="http://skillsmatter.com/event/agile-scrum/agile-testing-bdd-exchange-2010">Agile Testing &amp; BDD eXchange</a>. In one of the presentations Erik Stenman talked about Erlang amongst other things and caught my attention.<span id="more-17"></span></p>
<p>Now I knew I have a problem. Too many languages and too little time. I realised that before I commit to a single language I want to have a bit of a nose around the programming languages landscape. So I thought, wouldn&#8217;t it be great if there was a book that would give you a crash course in several languages. Nothing too deep &#8211; only enough to present you the language&#8217;s philosophy and give you a feel of the language. Preferably these languages should cover different paradigms so that I could evaluate them as well and try to understand their strengths and weaknesses as well.</p>
<p>Well guess what a bit less than a month ago The Pragmatic Bookshelf have published <a href="http://pragprog.com/titles/btlang/seven-languages-in-seven-weeks">Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages by Bruce A. Tate</a>. From a bit of scan reading it seems that book is almost a perfect fit to my requirements. It promises to take you on a journey through seven languages in seven days (I&#8217;ll see if I can keep up) and languages are chosen to give you a flavour of object orientated programming, function programming, prototype based OO, logic programming and hybrids.</p>
<p>I will try to keep up with the program and tackle a new language each week. For those interested in my progress and findings I intend to blog about them as I go along. Wish me luck on my journey of discovery.</p>
]]></content:encoded>
			<wfw:commentRss>http://dalibornovak.co.uk/blog/seven-languages-in-seven-weeks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
