<?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>The Code Poet</title>
	<atom:link href="http://the.codepoet.ch/feed/" rel="self" type="application/rss+xml" />
	<link>http://the.codepoet.ch</link>
	<description>The garden of beautiful code</description>
	<lastBuildDate>Mon, 29 Oct 2012 16:41:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<atom:link rel='hub' href='http://the.codepoet.ch/?pushpress=hub'/>
		<item>
		<title>Ruby 2.0 Reaches Feature Freeze: What Will Change?</title>
		<link>http://the.codepoet.ch/ruby-2-0-reaches-feature-freeze-what-will-change/</link>
		<comments>http://the.codepoet.ch/ruby-2-0-reaches-feature-freeze-what-will-change/#comments</comments>
		<pubDate>Mon, 29 Oct 2012 16:41:50 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[2.0]]></category>
		<category><![CDATA[refinements]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[ruby-core]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=359</guid>
		<description><![CDATA[Yes, you read right: Ruby 2.0 is in feature freeze! This doesn&#8217;t mean it will be released in the next month. But it introduces us the most important changes that will come with version 2.0. The timing is actually pretty impressive, since this feature freeze was announcement by Yusuke Endoh one year ago! Ruby seems [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, you read right: <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/46258" title="Ruby Feature Freeze Announcement">Ruby 2.0 is in feature freeze</a>! This doesn&#8217;t mean it will be released in the next month. But it introduces us the most important changes that will come with version 2.0.</p>
<p>The timing is actually pretty impressive, since this feature freeze was announcement by Yusuke Endoh one year ago! Ruby seems to be on track (pun totally intended)! Want more details? Read on!</p>
<h3>The Release Plan</h3>
<p>Right now, there are two important milestones left: Code freeze and release. Code freeze is planned at the end of the year. So all the bugs should be more or less ironed out by then.</p>
<p>And then, on February 02, 2013, <a href="http://en.wikipedia.org/wiki/Ruby_(programming_language)#Choice_of_the_name_.22Ruby.22" title="Rubys First Words!">20 years after Ruby became part of this world</a>, <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/40301" title="Ruby 2.0 Release Plan">Ruby 2.0 should be released</a>!</p>
<h3>The New Features</h3>
<p>There isn&#8217;t yet an official list out with all the new features, but the source code, commit history and discussions are open to the public, so it is no secret!</p>
<p>This isn&#8217;t a big release as the change from 1.9 to 2.0 would suggest, there are only a handful of new features:</p>
<h3>Refinements</h3>
<p>Refinements are one of the more interesting changes that could actually change the way Ruby code is written. <a href="http://bugs.ruby-lang.org/issues/show/4085" title="Ruby Bug Tracker: Refinements">It allows you to monkey patch a class</a>, but only where you want to, and not in the global scope.</p>
<p>Monkey patching could actually become an accepted thing to do!</p>
<p>Some code? Sure:</p>
<p></p><pre class="crayon-plain-tag">module StringExt
  refine String
    def length
      42
    end
  end
end</pre><p> </p>
<p>Here we define a module that contains a refinement (<pre class="crayon-plain-tag">refine</pre>) for String. Because length should always be 42 (for whatever reason&#8230;). Let&#8217;s define a class that could use it:</p><pre class="crayon-plain-tag">class TheAnswerToLifeTheUniverseAndEverything
  def self.answer(the_question)
    the_question.length
  end
end</pre><p> </p>
<p>But that is wrong! And that&#8217;s right. No, wait, I mean, the <strong>answer</strong> is wrong, but the rest is right. That&#8217;s because we didn&#8217;t use the refinement yet, we just defined it.</p>
<p>Let&#8217;s use it then:</p><pre class="crayon-plain-tag">class TheAnswerToLifeTheUniverseAndEverything
  using StringExt

  def self.answer(the_question)
    the_question.length
  end
end</pre><p> </p>
<p>If we ask for <pre class="crayon-plain-tag">TheAnswerToLifeTheUniverseAndEverything.answer("I want the answer to everything!!! NOOOOOW")</pre> it will tell us 42. Exactly what we want. But the behavior of <pre class="crayon-plain-tag">String#length</pre> outside of our class hasn&#8217;t changed. At all! Only when we use it inside class that is using this refinement is the behavior changed.</p>
<p>Which could be really handy!</p>
<h3>Named Arguments</h3>
<p>Another one that we might end seeing a lot is named arguments. Named arguments could replace the options hash that is so prevalent in todays Ruby code. Use  <pre class="crayon-plain-tag">def namedParameters?(yes: true, no: false) [...] end</pre>.</p>
<p>Then there are some internal changes and some speed improvements for MRI. Nothing exciting, but definitely good changes.</p>
<h3>Will It Break Stuff?</h3>
<p>Most likely? No. The most likely candidate to break stuff? <pre class="crayon-plain-tag">respond_to?</pre>.</p>
<p>The behavior of <pre class="crayon-plain-tag">respond_to?</pre> in regards to protected methods has changed. By default, in 2.0, it doesn&#8217;t search for private or protected methods, in Ruby 1.9 it only excludes private methods. <a href="http://tenderlovemaking.com/2012/09/07/protected-methods-and-ruby-2-0.html" title="Tenderlove on respond_to? in Ruby 2.0">The amazing Tenderlove has a great writeup about this</a>.</p>
<p>Apart from that, it seems like there is nothing else that will break with Ruby 2.0, so the upgrade should be easier then going from 1.8 to 1.9. <a href="https://bugs.ruby-lang.org/projects/ruby-trunk/roadmap#2.0.0" title="Rubys 2.0 Roadmap">Even though the roadmap is actually pretty big!</a></p>
<h3>Yay! Exciting Stuff!</h3>
<p>I&#8217;m looking forward to Ruby 2.0. I&#8217;m especially interested in what kind of clever hacks people will come up with now that we have refinements.</p>
<p>What are you looking forward to? What is still missing in your opinion?</p>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/ruby-2-0-reaches-feature-freeze-what-will-change/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Does Rails Still Deserve To Be The Default Web Framework In Ruby?</title>
		<link>http://the.codepoet.ch/does-rails-still-deserve-to-be-the-default-web-framework-in-ruby/</link>
		<comments>http://the.codepoet.ch/does-rails-still-deserve-to-be-the-default-web-framework-in-ruby/#comments</comments>
		<pubDate>Sat, 13 Oct 2012 21:23:38 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[camping]]></category>
		<category><![CDATA[cramp]]></category>
		<category><![CDATA[cuba]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sinatra]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=352</guid>
		<description><![CDATA[To answer the question directly (aka a TLDR here for the impatient): I don&#8217;t know. Read on! (HA! Tricked you there!) Let&#8217;s take a dive through some Ruby web frameworks. There is one or two around, even if we don&#8217;t count Rails. I promise! But First, Let&#8217;s Take The Rails You know, I&#8217;m from Switzerland. [...]]]></description>
			<content:encoded><![CDATA[<p>To answer the question directly (aka a TLDR here for the impatient): I don&#8217;t know. Read on! (HA! Tricked you there!)</p>
<p>Let&#8217;s take a dive through some Ruby web frameworks. There is one or two around, even if we don&#8217;t count Rails. I promise!</p>
<h3>But First, Let&#8217;s Take The Rails</h3>
<p>You know, I&#8217;m from Switzerland. We love the Railway here. We take it everywhere. It&#8217;s our go to solution. Why? It offers everything we need, it is on time, and we can precisely plan when we will be at the destination.</p>
<p>Like Rails! It is the go to solution, exactly because it offers so much stuff. Want migrations? It&#8217;s right there! Want an asset pipeline? It&#8217;s right there! Want to use SASS? It&#8217;s right there! Architecture? Done. Validations? Done. ORM? Done. Security? Done.</p>
<p>This is really nice! And we know that it might not be the quickest way to get somewhere, but it will surely get us there in a hell of a good time and still can continue from there. There are no dead ends!</p>
<p><em>If you are American, I&#8217;m sorry. I know you probably don&#8217;t get why calling Rails after the Railway is a good and apt metaphor. So come visit Switzerland, get a good local beer with me and I will show you why!</em></p>
<h3>Onwards, To Cuba!</h3>
<p>Cuba! Where you can really do whatever you want. Welcome to the island of simpler times. The island where everything still seems easy.</p>
<p>Cuba doesn&#8217;t have too many complicated processes. Everything can be done, as long as you do it yourself.</p>
<p>Welcome to <a href="https://github.com/soveran/cuba" title="Cuba" target="_blank">Cuba</a>, the Ruby microframework. Using Cuba is real easy:</p><pre class="crayon-plain-tag">require "cuba"

Cuba.use Rack::Session::Cookie

Cuba.define do
  on get do
    on "hello" do
      res.write "Hello world!"
    end

    on root do
      res.redirect "/hello"
    end
  end
end</pre><p> </p>
<p>Yes, that&#8217;s it! Inspired by <a href="http://www.sinatrarb.com/" title="Sinatra" target="_blank">Sinatra</a>? Maybe. It offers a few other things, like security or template handling through <a href="https://github.com/rtomayko/tilt" title="Tilt" target="_blank">Tilt</a>.</p>
<p>A very simple, and laid back framework. Very fitting for Cuba.</p>
<h3>Lets Go Camping</h3>
<p>But of course, now that we are in Cuba, let&#8217;s go camping! This way, we can see the landscape. We can see the world like it really is, unfiltered. And it is healthy, too!</p>
<p>Don&#8217;t know how to set up your tent? No problem, maybe the <a href="http://camping.io/" title="Camping" target="_blank">Camping</a> framework can help:</p><pre class="crayon-plain-tag">Camping.goes :Tent

module Tent::Models
  class Tarpaulin &lt; Base; end
end

module Tent::Controllers
  class Index
    def get
      @tarpaulins = Tarpaulin.all
      render :index
    end
  end
end

module Tent::Views
  def index
    @tarpaulins.each do |tarpaulin|
      h1 tarpaulin.size
      div { tarpaulin.color }
    end
  end
end</pre><p> </p>
<p>Yep, that&#8217;s it. Very Rails inspired, isn&#8217;t it? But it makes sense, because going camping with the train makes a lot more sense! And yes, it is all in one file. Including MVC, including routes, including everything!</p>
<p>But will it replace Rails? I don&#8217;t think so.</p>
<h3>All That Traveling Gives Me A Cramp</h3>
<p>Already? A cramp? We just traveled a bit around Cuba and you have already a cramp? I guess it was time to get you out of your comfort zone!</p>
<p>Ah, that damn cramp. This is something special. But lucky for us, we don&#8217;t have to wait until your cramp recedes, we can just continue. Nice, isn&#8217;t it?</p>
<p>Don&#8217;t know why we can continue even while you have a <a href="http://cramp.in/" title="Cramp" target="_blank">Cramp</a>? See for yourself:</p><pre class="crayon-plain-tag">require 'cramp'

class GoHomeAction &lt; Cramp::Action
  def start
    render "Let's Go Home"
    finish
  end
end</pre><p> </p>
<p>Yup, that&#8217;s it. The cool thing about Cramp? It is designed for asynchronous requests right out of the box. It is very simple, but powerful, if used the right way.</p>
<p>But, since walking around with a cramp isn&#8217;t fun, maybe it&#8217;s time that we head home.</p>
<h3>Home Sweet Home</h3>
<p>Welcome back! I hope you enjoyed this tour around the (Ruby) world. I know I did! So to answer the question that the title was asking all along: Probably not, since all these other frameworks target another niche.</p>
<p>Will something replace Rails? I don&#8217;t know. Rails has a lot of convenience built it that it just a burden to lose. And I don&#8217;t think we will see a new full featured web framework soon. But these micro frameworks are really nice for the small niche that they chose to fill.</p>
<p>So for very small apps, maybe Rails isn&#8217;t the right choice. For bigger ones? I still think the convenience of Rails can&#8217;t be topped by these small frameworks.</p>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/does-rails-still-deserve-to-be-the-default-web-framework-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Microsoft, Why? (aka Introducing TypeScript)</title>
		<link>http://the.codepoet.ch/why-microsoft-why-aka-introducing-typescript/</link>
		<comments>http://the.codepoet.ch/why-microsoft-why-aka-introducing-typescript/#comments</comments>
		<pubDate>Mon, 08 Oct 2012 16:18:04 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[typescript]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=344</guid>
		<description><![CDATA[Interesting. Microsoft recently released their own version of JavaScript, TypeScript! Now, some people might want to mock Microsoft for that. Maybe they just couldn&#8217;t make Visual Studio play nice with JavaScript, so they made TypeScript. Oh wait! I am one of those people! Naah, let&#8217;s be serious here. TypeScript is something like CoffeeScript. It does [...]]]></description>
			<content:encoded><![CDATA[<p>Interesting. Microsoft recently released their own version of JavaScript, <a href="http://www.typescriptlang.org/" title="TypeScript" target="_blank">TypeScript</a>! Now, some people might want to mock Microsoft for that. Maybe they just couldn&#8217;t make Visual Studio play nice with JavaScript, so they made TypeScript. Oh wait! <a href="http://twitter.com/codepoet_ch/status/253329673252118530" title="Me mocking TypeScript" target="_blank">I <strong>am</strong> one of those people!</a></p>
<p>Naah, let&#8217;s be serious here. TypeScript is something like CoffeeScript. It does less and more. It offers &#8220;proper&#8221; type support and a class system. They don&#8217;t touch any syntax however.</p>
<p><em>At first I simply wanted to introduce TypeScript, but it turned out in fact I just wanted to rant about how wrong Microsoft is once again in trying to introduce their own standards.</em></p>
<h3>An Example, Please!</h3>
<p>Sure, judge for yourself:</p>
<p></p><pre class="crayon-plain-tag">class Greeter {
	greeting: string;
	constructor (message: string) {
		this.greeting = message;
	}
	greet() {
		return "Hello, " + this.greeting;
	}
}   

var greeter = new Greeter("world");

var button = document.createElement('button')
button.innerText = "Say Hello"
button.onclick = function() {
	alert(greeter.greet())
}

document.body.appendChild(button)</pre><p>(Taken straight from their <a href="http://www.typescriptlang.org/Playground/" title="TypeScript Example" target="_blank">documentation</a>)</p>
<p>So this is basically JavaScript syntax. All braces are kept intact. Except it introduces classes and types, that will be checked during compilation.</p>
<h3>So, Why?</h3>
<p>Apparently, <a href="http://www.zdnet.com/who-built-microsoft-typescript-and-why-7000005206/" title="Why Microsoft Made TypeScript" target="_blank">some people working for Microsoft had problems with JavaScript</a>. Which is fair, since I guess we all have problems with JavaScript sometimes. Microsoft solution? Make our own JavaScript!</p>
<p>Because, let&#8217;s be honest, this worked so well with a lot of stuff they did, like, oh, ActiveX&#8230; or OOXML.</p>
<h3>Why Does Microsoft Need Its Own Stuff Again?</h3>
<p>I honestly have no idea. No idea whatsoever! It doesn&#8217;t make any sense. We have CoffeeScript. If they wanted to back a JavaScript replacement with types, why not back <a href="http://www.dartlang.org/" title="Dart by Google" target="_blank">Google&#8217;s Dart</a>? It would make a lot more sense.</p>
<p>But no, Microsoft had to introduce its own stuff again. Why will remain a mystery, probably forever.</p>
<h3>Will This Succeed?</h3>
<p>Probably. Why? Simply because in Microsoft world, people love Microsoft stuff. Nothing wrong with that, it is convenient. And if Microsoft backs something, this means people will probably pick it up.</p>
<p>The integration into Visual Studio, the IDE of choice for Microsoft programmers, will probably have superb support for TypeScript, including refactoring and all the other goodies that people expect from IDEs these days.</p>
<p>So yes, I think  a few people will pick it up, just because it is bundled. The same way that a lot of Rubyists picked up SASS, or CoffeeScript, simply because it comes bundles with Rails. And yes, I know, not every Rubyist uses Rails, sorry &#8217;bout that!</p>
<h3>You Don&#8217;t Like It, Right?</h3>
<p>Well, no. It doesn&#8217;t offer enough new stuff that we could consider this a new awesome thing that could change the landscape of web programming. In fact, I don&#8217;t think it offers anything new.</p>
<p>So no, I don&#8217;t like it. I don&#8217;t like it when companies waste good, sparse programming talent. Why couldn&#8217;t Microsoft back Dart? Than that language would suddenly be huge! If Chrome and Internet Explorer come with proper Dart support, Mozilla would have no choice but to back it too!</p>
<p>But no, this way we, again, have multiple &#8220;standards&#8221; that aren&#8217;t interoperable. We will have another fight on our hands with Google pushing for Dart, Microsoft for TypeScript, and Mozilla (maybe) for CoffeeScript. <a href="http://html5doctor.com/multimedia-troubleshooting/#browser-support" title="The Video Codec Mess" target="_blank">Does this remind anyone else of web codecs?</a></p>
<p>No, no, and no. I do not agree with that decision by Microsoft. I think its wrong, bad, and it definitely won&#8217;t help move the web forward. In fact, it pushes it backwards.</p>
<p>So sorry. No.</p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/why-microsoft-why-aka-introducing-typescript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is The Time of The Big Languages Coming To An End?</title>
		<link>http://the.codepoet.ch/is-the-time-of-the-big-languages-coming-to-an-end/</link>
		<comments>http://the.codepoet.ch/is-the-time-of-the-big-languages-coming-to-an-end/#comments</comments>
		<pubDate>Mon, 01 Oct 2012 15:48:58 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=198</guid>
		<description><![CDATA[Go, Rust, Erlang, R, Closure and what all the new(er) languages around the block are all called. But one thing is certain: They are all niche languages. Why is that? Why don&#8217;t we have another C or Java? One language to rule them all? If you have ever chosen a new language to program in, [...]]]></description>
			<content:encoded><![CDATA[<p>Go, Rust, Erlang, R, Closure and what all the new(er) languages around the block are all called. But one thing is certain: They are all niche languages. Why is that? Why don&#8217;t we have another C or Java? One language to rule them all?</p>
<p>If you have ever chosen a new language to program in, you know the steps: Look at whats available, dabble in them, find out what other people think about it. Or you might do it like I did. Chose a framework, and then learn the language that this framework is written in. This way I got started in Ruby.</p>
<h3>Big Languages Are The Past, Right?</h3>
<p>But is the time for big languages, ones that we use for everything, coming to an end? Maybe. The last &#8220;big&#8221; language is C#, and just because it got a lot of promotion from Microsoft. Before that? Java. C. Cobol. Let&#8217;s not go further into the past please!</p>
<p>And while no new all purpose language has come out, we get more and more niche languages. R for data processing. Rust for systems programing. ClosureScript for writing web applications in a LISP dialect.</p>
<h3>Programming Languages Have A Short Lifespan</h3>
<p>Is the time of the all purpose programming language coming to an end? I think so. There was a time when a company would use one language. One framework. One database. One operation system.</p>
<p>But today? Employees chose their operation system. Databases are mix and matched freely for whatever purpose they serve best. And programming languages are intermingled in huge systems.</p>
<h3>The Programming Language Lifecycle</h3>
<p>Programming languages seem to have a limited lifetime. They take a few years to mature, are then picked up, and a few more years later, replaced by something new. </p>
<p>And I think we should all embrace this change. Because we learn a lot about language design, programming. CPUs get faster and faster, so the language designers can get away with even more every year.</p>
<p>Is this cycle inevitable? Yes. It is. There are some languages which try to dump every new thing into it (hello Java, hello C#!).</p>
<p>But look at what happens when you just put everything in your fridge in a pan and let it cook for an hour. You get crap! The same thing happens to languages as well. The only fix is to do something new, which takes full advantage of that new paradigm.</p>
<h3>No Good Programmer Knows Just One Language</h3>
<p>Programmers who can just use one language are becoming a rarity. Does who do will soon be obsolete. So don&#8217;t think just because you use Ruby you are save. Ruby is quite popular right now, so according to the language life cycle, it will soon be made obsolete by something better.</p>
<p>Will everyone switch to the new thing? Of course not, there is a good reason why big companies still search for Cobol programmers.</p>
<p>But those of us who want to stay ahead, who want to belong the circle of not just good programmers, but great programmers. We can&#8217;t look the other way. We can&#8217;t ignore all new languages and stay too comfy with our current languages.</p>
<blockquote><p>Don&#8217;t have a holy cow that you aren&#8217;t ready to slaughter!</p></blockquote>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/is-the-time-of-the-big-languages-coming-to-an-end/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Are Your Abusing Constructors, Too?</title>
		<link>http://the.codepoet.ch/are-your-abusing-constructors-too/</link>
		<comments>http://the.codepoet.ch/are-your-abusing-constructors-too/#comments</comments>
		<pubDate>Mon, 24 Sep 2012 15:36:29 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Theory]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[organization]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=318</guid>
		<description><![CDATA[Ah, constructors. The little, forgotten, and often abused thing. No one really thinks about it for long, no one really knows what it does. Everyone tends to ignore it until they think they need it and throws things into it with no care whatsoever. Yes, the constructor might be the best equivalent to a homeless [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, constructors. The little, forgotten, and often abused thing. No one really thinks about it for long, no one really knows what it does. Everyone tends to ignore it until they think they need it and throws things into it with no care whatsoever. Yes, the constructor might be the best equivalent to a homeless child.</p>
<p>And yet, recently, I started thinking more and more about <del datetime="2012-09-23T18:24:12+00:00">homeless children</del> constructors. They are a defining character of all object-oriented programming languages. And they do fulfill an important role! So let&#8217;s learn a bit more about the homeless constructor.</p>
<h3>Do&#8217;s in a Constructor</h3>
<p>Ever had a class that felt awkward to use? Where you, in practically ever method, first checked if some precondition was fulfilled? That might mean you have forgotten to put something in a constructor.</p>
<p>The simple rule is: <strong>Everything the class needs in order to work goes into the constructor.</strong> For example, recently I wrote a small proxy to <a href="http://nokogiri.org/" title="Nokogiri" target="_blank">Nokogiri</a>. The class looked something like this:</p><pre class="crayon-plain-tag">class XmlHelper
  def open(file)
    @file = Nokogiri::XML(file)
  end

  def find_uas
    raise 'Open the file first' unless @file
    # read file etc.
  end
end</pre><p> </p>
<p>With one method, that might work. But how would I add a second method here? I would have to do the same check if the file is actually opened again. Which means <strong>the class isn&#8217;t ready for operation after the constructor is called!</strong> So I fixed this the easy (but hey, stylish!) way:</p><pre class="crayon-plain-tag">class XmlHelper
  def initialize(file)
    @file = Nokogiri::XML(file)
  end

  def find_uas
    raise 'Open the file first' unless @file
    # read file etc.
  end
end</pre><p> </p>
<p>Yes, one small change. From now on, when someone wants to make an instance of XmlHelper, he <strong>has to provide a file to read.</strong> Which means the class is ready to be used.</p>
<blockquote><p>Put things into the constructor if the class can&#8217;t feasibly be used without it.</p></blockquote>
<p>Another great use of constructors? Dependencies! Of course, you could add a dependency like that:</p>
<p></p><pre class="crayon-plain-tag">class Limb
  @child = Child.new
end</pre><p> </p>
<p>We have two problems with this:</p>
<ol>
<li>The Limb class knows too much about its surroundings. Maybe it just needs to tell the child that it is tired. But what if we suddenly want to use the Limb class with an Adult? A dog? We can&#8217;t do that with this implementation!</li>
<li>Another problem is testing. We can&#8217;t, in a unit test, easily replace Child with a mock or a stub. Maybe instantiation of a child is difficult (it takes 9 months for gods sake!) Or we simply want to test the interaction between Limb and Child, which is difficult if we don&#8217;t know to which Child this Limb is talking to. (Remember: <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/" title="Flaw: Constructor does Real Work" target="_blank">A constructor shouldn&#8217;t do real work!</a>)</li>
</ol>
<p>The better way to do it?</p>
<p></p><pre class="crayon-plain-tag">class Limb
  def initialize(<span class="hiddenGrammarError" pre="">body)
    @body</span> = body
  end
end</pre><p> </p>
<p>Ah, now it&#8217;s obvious, right? We can simply add a new body that this Limb belongs to. And the Limb doesn&#8217;t care anymore if it is a child or not. For the lazy who don&#8217;t want to type  <pre class="crayon-plain-tag">Limb.new(child)</pre> all the time, we can also change the initialize call to  <pre class="crayon-plain-tag">def initialize(body = Child.new) ... end</pre> so the Child is automatically added if we don&#8217;t specify something else.</p>
<h3>But Don&#8217;t Put These Things Into the Constructor!</h3>
<p>So now that we have a usable object after construction, what doesn&#8217;t belong in there? Should we just throw everything into there?</p>
<p>Now that we know what to throw into a constructor, it is easy what not to throw into a constructor. In short: Everything else. If not every method in the class needs this property, if the class doesn&#8217;t need this information to work correctly, then don&#8217;t add it to the constructor! If some methods need a property, and the others don&#8217;t, you probably should split the class up into two. <a href="http://the.codepoet.ch/what-you-should-know-about-solid/" title="What you should know about SOLID">They deal with different things.</a></p>
<p>One special thing though: If the initialization logic is very complex, it might be better to put it into a builder. A builder is a separate object that can be used to build up an object. It can even build objects of different classes (that have the same method, of course) given different input.</p>
<h3>Ah, So That&#8217;s Constructors!</h3>
<p>Yes, so it is. So please stop abusing the little homeless kid on the block and treat it with the kindness it deserves. It shouldn&#8217;t be dropped all responsibilities, but don&#8217;t forget that it is there and you can use it.</p>
<p>To summarize: <strong>Pass a constructor all the information the class needs to work properly, but nothing more.</strong></p>
<p>Have I forgotten some use case for constructors? Do you know some other constructor abuse cases? Then drop a line either here in the comments or on <a href="https://twitter.com/codepoet_ch" title="On Twitter" target="_blank">Twitter</a>!</p>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/are-your-abusing-constructors-too/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>rspec-given Shows How RSpec Can Be Extended</title>
		<link>http://the.codepoet.ch/rspec-given-shows-how-rspec-can-be-extended/</link>
		<comments>http://the.codepoet.ch/rspec-given-shows-how-rspec-can-be-extended/#comments</comments>
		<pubDate>Mon, 17 Sep 2012 05:21:50 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Code Reading]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[rspec-given]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=301</guid>
		<description><![CDATA[Just recently Jim Weirich released rspec-given 2.0, an extension to the RSpec DSL. It tries to add [crayon-51b9f31486e6c-i/], [crayon-51b9f3148bc8f-i/], [crayon-51b9f31490aaa-i/] semantics to RSpec. Why? Because [crayon-51b9f314958bc-i/], [crayon-51b9f3149a70e-i/], [crayon-51b9f3149f4fc-i/] state intent. So everything in a [crayon-51b9f314a4318-i/] will be something that you expect to be present. [crayon-51b9f314a9143-i/] is the part that should actually be tested and [crayon-51b9f314adf5b-i/] [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently <a href="https://twitter.com/jimweirich" title="Jim Weirich on Twitter" target="_blank">Jim Weirich</a> released <a href="https://github.com/jimweirich/rspec-given" title="rspec-given" target="_blank">rspec-given 2.0</a>, an extension to the RSpec DSL. It tries to add <pre class="crayon-plain-tag">given</pre>, <pre class="crayon-plain-tag">when</pre>, <pre class="crayon-plain-tag">then</pre> semantics to RSpec. Why? Because <pre class="crayon-plain-tag">given</pre>, <pre class="crayon-plain-tag">when</pre>, <pre class="crayon-plain-tag">then</pre> state <strong>intent</strong>. So everything in a <pre class="crayon-plain-tag">given</pre> will be something that you expect to be present. <pre class="crayon-plain-tag">when</pre> is the part that should actually be tested and <pre class="crayon-plain-tag">then</pre> states what should happen.</p>
<p>This makes it easier to see what is just a test helper, which code is actually under test and what should happen to the different moving parts. Go read the <a href="https://github.com/jimweirich/rspec-given/blob/master/README.md" title="rspec-given readme" target="_blank">readme in the rspec-given repository</a>, Jim does a way more thorough job at explaining the semantics. Or if you know Cucumber, you can just continue! For the rest, I will be waiting, right here.</p>
<p>Back already? Good! Let&#8217;s continue!</p>
<h3>A Primer on RSpec Given</h3>
<p>So, if we want to write a Math library that uses a very strange notation, a spec could look like this:</p><pre class="crayon-plain-tag">describe "Math" do
	describe "#+" do
		it "adds two values" do
			let(:value_one) { 2 }
			let(:value_two) { 3 }

			Math.+ value_one, value_two

			Math.result.should == 5
		end
	end
end</pre><p> </p>
<p>I know, I know, it&#8217;s a very contrived example, sorry&#8230; <img src='http://the.codepoet.ch/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>But what are we testing now? <pre class="crayon-plain-tag">Math.+</pre> or <pre class="crayon-plain-tag">Math.result</pre>?</p>
<p>With rspec-given, we could specify intent a lot clearer:</p><pre class="crayon-plain-tag">describe "Math" do
	describe "#+" do
		context "adds two values" do
			Given(:value_one) {2}
			Given(:value_two) {3}

			When { Math.+ value_one, value_two }

			Then { Math.result.should == 5 }
		end
	end
end</pre><p> </p>
<p>So we have two preconditions (setting <pre class="crayon-plain-tag">value_one</pre> and <pre class="crayon-plain-tag">value_two</pre>), which are just there to help the test. Then we have the code we actually want to test (<pre class="crayon-plain-tag">Math.+</pre>). And we have a post condition, that needs to be true at the end. But in the second example it is clear that we don&#8217;t test <pre class="crayon-plain-tag">Math.result</pre>, but <pre class="crayon-plain-tag">Math.+</pre>! Also notice that <pre class="crayon-plain-tag">then</pre> replaces the need for an <pre class="crayon-plain-tag">it</pre> block.</p>
<p>I guess we all can agree that the second example is way more specific which code is there for what purpose. Which I like.</p>
<p>But that got me to ask one question: How do you extend the DSL? Let&#8217;s see:</p>
<h3>Extending DSLs</h3>
<p>First things first (because it wouldn&#8217;t make sense to mention the first thing at the end I guess? That saying has never made sense to me, sorry&#8230;), I&#8217;m looking at rspec-given-2.0.0.beta3. This way, Jim can&#8217;t ninja change things in there and then tell me I&#8217;m totally wrong.</p>
<p>Second things second (that doesn&#8217;t exist, does it?): Let&#8217;s have a look at the first file: <a href="https://github.com/jimweirich/rspec-given/blob/rspec-given-2.0.0.beta.3/lib/rspec/given.rb" title="rspec/given" target="_blank">rspec/given.rb</a>. If we ignore RSpec 1 (someones still using that?), we include different files. One of the interesting files is configure.rb. Here the important lines (actually, all lines in this file except the includes):</p>
<p></p><pre class="crayon-plain-tag">RSpec.configure do |c|
  c.extend(RSpec::Given::ClassExtensions)
  c.include(RSpec::Given::InstanceExtensions)
  c.include(RSpec::Given::HaveFailed)
end</pre><p>(<a href="https://github.com/jimweirich/rspec-given/blob/rspec-given-2.0.0.beta.3/lib/rspec/given/configure.rb#L5-9" title="configure.rb" target="_blank">On Github</a>)</p>
<p>He uses RSpec.configure to extend and include a few extensions. If we take a look at RSpecs rubydocs, we can find <a href="http://rubydoc.info/github/rspec/rspec-core/RSpec#configure-class_method" title="RSpec.configure" target="_blank">RSpec.configure</a>. Even more important, the Configuration object has two methods: <pre class="crayon-plain-tag">extend</pre> and <pre class="crayon-plain-tag">include</pre>! Now that&#8217;s handy, because that&#8217;s exactly what we used!</p>
<p>These two are really similar. <a href="http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#extend-instance_method" title="Configure#extend" target="_blank">#extend</a> is used to add methods to example groups (but not the examples itself). It is added to the <pre class="crayon-plain-tag">describe</pre> block, but not the <pre class="crayon-plain-tag">it</pre> block.</p>
<p>Now guess what <a href="http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#include-instance_method" title="Configure#include" target="_blank">#include</a> does. Right! It adds methods to the example itself, but not to the example group!</p>
<p>Now we know how we can add custom stuff to RSpec examples and example groups! Which is exactly what rspec-given does!</p>
<p>The class and instance extensions can be seen in <a href="https://github.com/jimweirich/rspec-given/blob/rspec-given-2.0.0.beta.3/lib/rspec/given/extensions.rb" title="extensions.rb" target="_blank">extensions.rb</a>.</p>
<h3>That&#8217;s How You Extend a DSL</h3>
<p>RSpec nicely enough provided everything we need to extend it&#8217;s syntax with our own methods. Of course this isn&#8217;t just handy for rspec-given. Everyone can extend RSpec with its own syntax, which might come in handy once in a while.</p>
<p>I suggest you have a look at the <a href="http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration" title="RSpec Configuration" target="_blank">RSpec Configuration object</a>, because it can actually do  a lot. Did you know that it can show you the 10 slowest examples, automatically? Or that RSpec has <a href="http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks" title="RSpec Hooks" target="_blank">before, after, and around hooks</a>?</p>
<p>So not only have we learned what rspec-given is, how it works, but also how we can extend RSpec ourselves! Thanks Jim!</p>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/rspec-given-shows-how-rspec-can-be-extended/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Please Don&#8217;t Follow Advice From The Experts!</title>
		<link>http://the.codepoet.ch/please-dont-follow-advice-from-the-experts/</link>
		<comments>http://the.codepoet.ch/please-dont-follow-advice-from-the-experts/#comments</comments>
		<pubDate>Mon, 10 Sep 2012 07:24:16 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[advice]]></category>
		<category><![CDATA[expert]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=276</guid>
		<description><![CDATA[Fun Fact: You most likely fall into the same trap that I do: The expert&#8217;s dilemma! The Expert&#8217;s Dilemma? Yes, the expert&#8217;s dilemma. What is it, you might ask. In fact, if you don&#8217;t ask yourself this question right now, why are you even reading? Maybe this piece then isn&#8217;t for you. If you are, [...]]]></description>
			<content:encoded><![CDATA[<p>Fun Fact: You most likely fall into the same trap that I do: The expert&#8217;s dilemma!</p>
<h2>The Expert&#8217;s Dilemma?</h2>
<p>Yes, the expert&#8217;s dilemma. What is it, you might ask. In fact, if you don&#8217;t ask yourself this question right now, why are you even reading? Maybe this piece then isn&#8217;t for you. If you are, however, wondering what that is and why you most likely also fall into this trap, read on.</p>
<p>The expert&#8217;s dilemma is quite simple: We try to emulate experts as well as we can. We try to listen to their opinions, follow them, and carry out everything they say. The problem with that? If you are not an expert, <strong>most of the things they say don&#8217;t matter to you!</strong></p>
<p>Yes, seriously!</p>
<p>Let me illustrate through two examples: Fitness and Starcraft II. I know, I know, both of those have nothing to do with programming (except that progamer looks almost like programmer), but bear with me. Just a moment now.</p>
<h2>Let&#8217;s talk about Starcraft II</h2>
<p>Starcraft II is a really simple game. You have so-called harvesters, that gather resources. With these resources you can build more gatherers, units to attack or defend, or new structures and research to make everything stronger. Simple as that.</p>
<p>Now of course the pros are talking about balance, and that this unit might be a bit better if it had one attack power more than it has now. Or that if you delay action X, you can do action Y Z seconds faster. And other such fun things. If you follow all this advice, you fall into the trap of the expert&#8217;s dilemma.</p>
<p>When I started playing Starcraft II, it worked something like this:</p>
<p><em>Andreas:</em> &#8220;Wohoo, I play Starcraft II. Let&#8217;s just try to play so I have fun!&#8221;<br />
<em>The Pro:</em> &#8220;But Andreas, what are you doing? You should have a plan, you should scout your opponent!&#8221;</p>
<p><em>One week later</em><br />
<em>Andreas:</em> &#8220;Whohoo, I play Starcraft II. I even scout!&#8221;<br />
<em>The Pro:</em> &#8220;But Andreas, what are you doing? You should control each unit into his last detail!&#8221;</p>
<p><em>One week later</em><br />
<em>Andreas:</em> &#8220;Wohoo, I have a lot of free time!&#8221;</p>
<p>Why did that happen? Because I tried to follow all the experts advice, I forgot that I first need to learn the basics: Gathering resources! So I started to lose a lot and I quit.</p>
<p>So all the questions about balance and all that don&#8217;t play a role for people like me, because other things play a much bigger role. For the pros who play all in the same percentile, they do. But for me?</p>
<h2>Let&#8217;s talk about fitness</h2>
<p>Ah, fitness. Where everyone has an opinion. And everyone has a different opinion. It sucks! It makes it hard to start. So let&#8217;s take the journey for a normal beginner (like me!):</p>
<p><em>Andreas:</em> &#8220;Wohoo, I started with a fitness routine! So I run a few times per week and do some bodyweight exercises.&#8221;<br />
<em>The Pro:</em> &#8220;But Andreas, what are you doing? Running hurts your muscle gain, so stop that!&#8221;</p>
<p><em>One week later</em><br />
<em>Andreas:</em> &#8220;Wohoo, I started to do a lot of bodyweight exercises!&#8221;<br />
<em>The Pro:</em> &#8220;But Andreas, what are you doing? If you would vary them more, you could get better results! Also, start doing them on 5 days instead of 3.&#8221;</p>
<p><em>One week later</em><br />
<em>Andreas:</em> &#8220;Wohoo, I&#8217;m playing Starcraft II!&#8221;</p>
<p><em>Queue a rage comic here&#8230;</em></p>
<p>Result: Instead of following what I was doing and enjoying, I stopped doing anything. Instead of getting 80% of the results, I now get 0%!</p>
<h2>This Is A Blog About Programming, Right?</h2>
<p>Yes, absolutely. Now let me tie it back to programming.</p>
<p>TDD, BDD, refactoring, <a href="http://the.codepoet.ch/what-you-should-know-about-solid/" title="What You Should Know About SOLID">SOLID</a>, performance, method to line ration, line per class ratio, test coverage, newest trends, etc. etc. etc. Sounds familiar? Do you get it now? No?</p>
<p>Damn it!</p>
<p>That was the point of the whole post. </p>
<p>Let me try again: There are a lot of buzzwords and other things to follow, which can choke a beginner. A beginner should be doing. (<strong>Careful:</strong> Controversial statement following!) If it isn&#8217;t perfect, who cares?  <strong>It is more important to do than to do it perfectly!</strong></p>
<h2>Experts Are Nice, But&#8230;</h2>
<p>I know the experts just mean well. But a beginner or intermediate <insert what you want to do here> can&#8217;t possibly follow all advice! It will confuse her or him more than actually following through with it!</p>
<p>So don&#8217;t blindly follow experts advice, just keep being awesome and improving along the way!</p>
<p>Don&#8217;t get bogged down, don&#8217;t try to do it all perfectly from the beginning. Add stuff slowly and concentrate on the basics. Because like in Starcraft II and in fitness, it is better to do the basics right than butchering all the experts advice.</p>
<p>Did you fall into this trap yet? Share your (horror?) stories!</p>
<p><em>If you liked this post, you should consider <a href="http://the.codepoet.ch/hire-me/" title="Hire Me">hiring me freelance, part-time or full-time</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/please-dont-follow-advice-from-the-experts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What Is Your Rails Testing Stack?</title>
		<link>http://the.codepoet.ch/what-is-your-rails-testing-stack/</link>
		<comments>http://the.codepoet.ch/what-is-your-rails-testing-stack/#comments</comments>
		<pubDate>Mon, 03 Sep 2012 09:00:42 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[guard]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=270</guid>
		<description><![CDATA[Developing a Rails application is awesome. Why? Because the testing stack is really, really good! But, this is also a problem. There is so much to choose from, that it sometimes is really hard to figure out what is useful and what isn&#8217;t. I don&#8217;t know about you, but my Gemfile tends to have a [...]]]></description>
			<content:encoded><![CDATA[<p>Developing a Rails application is awesome. Why? Because the testing stack is really, really good! But, this is also a problem. There is so much to choose from, that it sometimes is really hard to figure out what is useful and what isn&#8217;t.</p>
<p>I don&#8217;t know about you, but my Gemfile tends to have a lot of stuff in the group <pre class="crayon-plain-tag">:development, :test</pre>.</p><pre class="crayon-plain-tag">group :development, :test do
  gem 'rspec-rails'
  gem 'cucumber-rails', :require =&gt; false
  gem 'database_cleaner'
  gem 'capybara'
  gem 'launchy'
  gem 'rr'
  gem 'awesome_print'
  gem 'jasminerice'
  gem 'guard-jasmine'
  gem 'poltergeist'
end</pre><p> </p>
<p>Holy crap that&#8217;s big! Of course, it also has some helpers only for development:</p><pre class="crayon-plain-tag">group :development do
  gem 'guard'
  gem 'libnotify'
  gem 'guard-spork'
  gem 'guard-cucumber'
  gem 'guard-rspec'
  gem 'guard-livereload'
  gem 'spork'
  gem 'pry-rails'
  gem 'pry-stack_explorer'
  gem 'pry-debugger'
end</pre><p> </p>
<p>Not too shaby, huh? So let me get into the details for each of those.</p>
<h3>Guard</h3>
<p><a href="https://github.com/guard/guard" title="Guard" target="_blank">Guard </a>is an amazing tool to automate nearly everything. Don&#8217;t believe me? Take a look at all the possible <a href="https://github.com/guard" title="Guard Extensions" target="_blank">Guard extensions on Github</a>! So what does Guard do?</p>
<p>It basically is a system that runs in the background and can watch files. Extensions tell it (through a <code>Guardfile</code>) that it should do something if a specific file pattern changes. For example, the <a href="https://github.com/guard/guard-cucumber" title="guard-cucumber" target="_blank">Cucumber guard</a> checks for this:</p><pre class="crayon-plain-tag">guard 'cucumber', :cli =&gt; "--drb" do
  watch(%r{^features/.+\.feature$})
  watch(%r{^features/support/.+$})          { 'features' }
  watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
end</pre><p> </p>
<p>It looks at all features in the features directory, and Guard will call the Cucumber guard if one of these files changes. In term, guard-cucumber will call Cucumber and show it to you on the console. Neat, isn&#8217;t it?</p>
<p>Of course, this isn&#8217;t just for Cucumber! I also use the <a href="https://github.com/guard/guard-rspec" title="guard-rspec" target="_blank">RSpec guard</a> (for RSpec tests, duh!), <a href="https://github.com/guard/guard-spork" title="guard-spork" target="_blank">Spork </a>and <a href="https://github.com/guard/guard-livereload" title="guard-livereload" target="_blank">livereload</a>.</p>
<p><a href="https://github.com/sporkrb/spork" title="Spork" target="_blank">Spork </a>is a DRb (Distributed Ruby) server that preloads the Rails environment so the tests run faster. Adding this guard makes Guard start the DRb server for Cucumber and RSpec automatically when starting Guard itself.</p>
<p>Another very neat guard is guard-livereload. Thanks to an extension in Chrome, this guard will automatically reload the page I have open when I change the ERb or the CSS files. Makes fiddling with CSS a lot easier!</p>
<h3>You Have To PRY It From My Cold Dead Hands</h3>
<p><a href="https://github.com/pry/pry" title="Pry" target="_blank">PRY </a>is one of those tools that you just have to know (and love). It is an alternative to IRB, and way better! Just add a  <pre class="crayon-plain-tag">binding.pry</pre> to your code and Pry will open in that context when the code runs over it. It allows you to change into classes and objects to inspect them more closely. And edit the methods if you want to!</p>
<p>Some additions I added to Pry (and there are a few <a href="https://github.com/pry/pry/wiki/Available-plugins" title="Pry Plugins" target="_blank">Pry plugins</a>) are <a href="https://github.com/nixme/pry-debugger" title="Pry Debugger" target="_blank">pry-debugger</a>, so I can step through code and see what it really does. To complement Pry Debugger, I also use <a href="https://github.com/pry/pry-stack_explorer" title="Pry Stack Explorer" target="_blank">pry-stack_explorer</a>. This plugin allows us to move up the stack and see the state of the code there.</p>
<p>Of course, in a perfect world, we never have to use one of these tools, but yeah&#8230; Real world and these things&#8230;</p>
<h3>Testing tools</h3>
<p>You might now say: &#8220;Hey! You said you will talk about <strong>testing</strong> tools, not support tools!&#8221; And you are right! (Though these support tools are amazing for testing!)</p>
<p>Yes, testing tools. For my testing needs I use the standard combo <a href="http://rspec.info/" title="RSpec" target="_blank">RSpec</a>, <a href="http://cukes.info/" title="Cucumber" target="_blank">Cucumber</a>, <a href="https://github.com/bmabey/database_cleaner" title="Database Cleaner" target="_blank">database_cleaner</a>, <a href="https://github.com/jnicklas/capybara" title="Capybara" target="_blank">Capybara</a> and <a href="https://github.com/btakita/rr" title="rr" target="_blank">rr</a> stack. I don&#8217;t think I have to explain these (Do I? If I do, you should tell me so in the comments!)</p>
<p>Additionally I use <a href="https://github.com/copiousfreetime/launchy" title="Launchy" target="_blank">launchy </a>to launch a browser window with the current state of the page to debug Cucumber steps.</p>
<p>I also make copious use of <a href="https://github.com/michaeldv/awesome_print" title="Awesome Print" target="_blank">awesome_print</a>, because it is, well, awesome! awesome_print can be used instead of <pre class="crayon-plain-tag">puts</pre> or <pre class="crayon-plain-tag">pp</pre> to output objects and have an easier time inspecting them. (Pry also can use awesome_print for it&#8217;s built in printing habits!)</p>
<h3>Last But Not Least: JavaScript Testing</h3>
<p>Something I struggled with for a bit, but found a good solution for, is JavaScript testing. There are two aspects here: One is Cucumbers Javascript support. For some acceptance tests, I needed Javascript to run. Per default, Cucumber uses Selenium (with a real browser), which is slow and not awesome at all! And then there are the Javascript unit tests, that also need to run.</p>
<p>For the unit tests I settled on <a href="http://pivotal.github.com/jasmine/" title="Jasmine" target="_blank">Jasmine</a>. Why? Because it seems it even tries to be as close to RSpec as possible, so I don&#8217;t have to learn yet another testing system. To run them, I of course, use a guard: <a href="https://github.com/netzpirat/guard-jasmine" title="guard-jasmine" target="_blank">guard-jasmine</a> (Who&#8217;s surprised?)</p>
<p>guard-jasmine uses <a href="http://phantomjs.org/" title="PhantomJS" target="_blank">PhantomJS </a>to run the Javascript tests in a headless mode (without a user interface), which makes them fast (not incredibly fast, but at least fast).</p>
<p>Of course we also want to integrate the Jasmine unit tests with the asset pipeline so we can use CoffeeScript. Luckily, Brad Phelan solved that problem and released <a href="https://github.com/bradphelan/jasminerice" title="JasmineRice" target="_blank">JasmineRice</a>.</p>
<p>Now, this doesn&#8217;t yet solve the Cucumber problem. For that, we can use <a href="https://github.com/jonleighton/poltergeist" title="Poltergeist" target="_blank">Poltergeist</a>. Poltergeist is a PhantomJS driver for Capybara. Do you see how this neatly matches up with guard-jasmine? Nice, isn&#8217;t it? I just configured Capybara to use Poltergeist instead of Selenium, and now the tests run at an acceptable rate.</p>
<h3>So, What&#8217;s Your stack?</h3>
<p>Yes, that is my &#8220;standard&#8221; Rails testing stack. I think this covers about everything I need. One thing that&#8217;s not included (because I currently don&#8217;t need it) is something like <a href="https://github.com/myronmarston/vcr" title="VCR" target="_blank">VCR</a>. VCR records HTTP interactions, so that during testing the HTTP calls don&#8217;t have to be made, but can be replayed by VCR. Which makes things even quicker.</p>
<p>Have I missed something? Added something superfluous? What do you use for your testing stack?</p>
<p><em>If you liked this post, you should follow me <a href="https://twitter.com/codepoet_ch" title="Twitter">on Twitter</a> and sign up for the <a href="http://feeds.feedburner.com/TheCodePoet" title="RSS feed">RSS feed</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/what-is-your-rails-testing-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>3 Things You Could Know If You Read &#8220;Eloquent Ruby&#8221; by Russ Olsen: A Review</title>
		<link>http://the.codepoet.ch/3-things-you-could-know-if-you-read-eloquent-ruby-by-russ-olsen-a-review/</link>
		<comments>http://the.codepoet.ch/3-things-you-could-know-if-you-read-eloquent-ruby-by-russ-olsen-a-review/#comments</comments>
		<pubDate>Mon, 27 Aug 2012 07:21:57 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Book Review]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=252</guid>
		<description><![CDATA[Starting with Ruby isn&#8217;t easy. Especially if you are like me. I have, like many before me, a Java background. So I knew statically typed languages, I knew what it meant, I knew what its strength were. And, like the monkey that I was, I thought statically typed languages were the best! Why? IDEs have [...]]]></description>
			<content:encoded><![CDATA[<p>Starting with Ruby isn&#8217;t easy. Especially if you are like me. I have, like many before me, a Java background. So I knew statically typed languages, I knew what it meant, I knew what its strength were.</p>
<p>And, like the monkey that I was, I thought statically typed languages were the best! Why? IDEs have a really easy time to refactor a lot of code. Everything was clear. Well, you know, the standard arguments you probably also here when we talk about statically typed languages.</p>
<p>But this post has nothing to do with statically typed languages, with me having seen the light at the of the tunnel and all that. And since you are reading this, I guess you did the same!</p>
<p>But, fun fact, here, for free, no sign-up needed: Ruby can do a lot more. A lot! And it has it warts, and it has its way of doing things. This isn&#8217;t easy to understand when you are knew, or even when you are experienced.</p>
<p>So here are 3 tips on what you could learn from reading <a href="http://www.amazon.com/gp/product/0321584104/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321584104&#038;linkCode=as2&#038;tag=thecode00-20" title="Eloquent Ruby" target="_blank">Eloquent Ruby</a> by <a href="https://twitter.com/russolsen" title="Russ Olsen on Twitter" target="_blank">Russ Olsen</a>:</p>
<h3>Strings In Ruby Are Awesome! Literally!</h3>
<p>Ever had to write a string in Java? I feel sorry for you, because I have written way too many back in the days. Remember  <pre class="crayon-plain-tag">"This is a \"String\""</pre> or even better, a Regex? (Hint:  <pre class="crayon-plain-tag">"\\..*"</pre>, and yes, that&#8217;s basically a smiley. That&#8217;s also my theory that smilies are a result of Java programmers using Regex).</p>
<p>The first string problem is easy to solve in Ruby: Just use a single quote! But even better, if you mix single and double quotes in a sentence (&#8220;And here shall &#8220;REST&#8221; Java. It&#8217;s done.&#8221;) we can solve this with a string literal: <pre class="crayon-plain-tag">%q{And here shall "REST" Java. It's done.}</pre>. See? No problem. In fact, we could even replace { with &#8216;$&#8217;, since we never use a &#8216;$&#8217; in our sentence! Or a lot of other special characters for that matter.</p>
<p>The Regex (<a href="http://googlefight.com/index.php?lang=en_GB&#038;word1=regex&#038;word2=regexp#" title="Google Fight Regex(p)" target="_blank">or Regexp</a>?) we could write as <pre class="crayon-plain-tag">/\..*/</pre>. No double escaping needed! And yes, it still resembles a smiley, I realize that. It&#8217;s still a Regex after all!</p>
<p>There is a lot more that can be done with string literals, so you should check that out in the book!</p>
<h3>Ruby Has Hooks For A Lot Of Things</h3>
<p>Ever written a module? Imagine that you are that module. This is carry stuff! Just think about it: You have to blindly stumble around, never knowing which class carries you around!</p>
<p>Luckily, Ruby offers us hooks that we can use to alleviate these problems. Each module has a <pre class="crayon-plain-tag">self.included</pre> method that you can use. It is called every time a class, well, includes this module. Even better, it even tells you which class it was!</p>
<p>This way you can build up a list of all classes that have this module included. Doesn&#8217;t sound very handy? But maybe, when the module is included, you also want to extend that class. Maybe you have a method that should not be on the class, but on the object!</p>
<p>With <pre class="crayon-plain-tag">self.included</pre> and other hooks, everything&#8217;s possible!</p>
<h3>How To Build Your Own DSL</h3>
<p>DSLs. How much we all love these little things, and how much we use them each and every day. Thank you, dynamic typing!</p>
<p>Probably you have at least seen some RSpec code. Yes, all that <pre class="crayon-plain-tag">let</pre>, <pre class="crayon-plain-tag">describe</pre>, and other syntactic sugar is just a DSL, implemented in Ruby. Do you want to know how to do this? Then go read the book! It has  a few caveats that I can&#8217;t all list here, because this is just a blog, not a book!</p>
<p>Even better, he also mentions how to build an external DSL. An external DSL is not written in Ruby, and has to be parsed &#8220;by hand&#8221; with Ruby (without the Ruby interpreter helping). Which does have its pros and cons, as you might imagine.</p>
<h3>Conclusion</h3>
<p>All in all, a very good book that opened my eyes (and still does, on my second reading of it!). One of the really nice features about the book is its structure. Russ gives us first an introduction why we want to do this, then tells us how. So far so good.</p>
<p>But then he continues: He also tells us how you can get in trouble with it. To top it off, he shows some examples of the described concept in the wild.</p>
<p>If you are a Rubyist and you haven&#8217;t yet read <a href="http://www.amazon.com/gp/product/0321584104/ref=as_li_ss_tl?ie=UTF8&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321584104&#038;linkCode=as2&#038;tag=thecode00-20" title="Eloquent Ruby" target="_blank">Eloquent Ruby</a>, I highly suggest you do. Because Ruby can do a lot more than you might image!</p>
<p><em>If you liked this post, you should follow me <a href="https://twitter.com/codepoet_ch" title="Twitter">on Twitter</a> and sign up for the <a href="http://feeds.feedburner.com/TheCodePoet" title="RSS feed">RSS feed</a>!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/3-things-you-could-know-if-you-read-eloquent-ruby-by-russ-olsen-a-review/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What Others Know About Compression That You Don&#8217;t</title>
		<link>http://the.codepoet.ch/what-others-know-about-compression-that-you-dont/</link>
		<comments>http://the.codepoet.ch/what-others-know-about-compression-that-you-dont/#comments</comments>
		<pubDate>Mon, 20 Aug 2012 06:38:43 +0000</pubDate>
		<dc:creator>Andreas Arnold</dc:creator>
				<category><![CDATA[Theory]]></category>
		<category><![CDATA[bzip2]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[deflate]]></category>

		<guid isPermaLink="false">http://the.codepoet.ch/?p=201</guid>
		<description><![CDATA[Ah, the compression formats. Always a mystery. Always just something we will use, but something we never understand. For one thing, this makes sense, considering Wikipedia alone lists 15 different compression formats. There are the well-known bz2, gz and LZMA algorithms. And the less well-known rz, ?Q? and ??_. Now that last one makes me [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, the compression formats. Always a mystery. Always just something we will use, but something we never understand. For one thing, this makes sense, considering Wikipedia alone <a href="http://en.wikipedia.org/wiki/List_of_archive_formats#Compression_only" title="Compression Formats on Wikipedia">lists 15 different compression formats</a>. There are the well-known <strong>bz2</strong>, <strong>gz</strong> and <strong>LZMA</strong> algorithms. And the less well-known <strong>rz</strong>, <strong>?Q?</strong> and <strong>??_</strong>. Now that last one makes me happy this isn&#8217;t a podcast&#8230;</p>
<p>Though for this post we will concentrate on <a href="http://en.wikipedia.org/wiki/Deflate" title="Deflate">Deflate</a>. Deflate is not only used in gzip and zip, but is also a fan favorite to compress web assets when sent to the browser.</p>
<h3>The Basic Building Blocks</h3>
<p>Underneath Deflate uses <a href="http://en.wikipedia.org/wiki/LZ77_and_LZ78" title="LZ77">LZ77</a> combined with a <a href="http://en.wikipedia.org/wiki/Huffman_coding" title="Huffman Coding">Huffman encoding</a>.</p>
<p>LZ77 is used to detect duplicate strings and insert a so-called back reference (more on this later). Huffman Encoding is similar to ASCII, but instead of always using 8 bits, it stores the most used characters in 3 bits, and the least used ones, with, well, more bits. More on this also later.</p>
<h3>Come On, Deflate Already!</h3>
<p>First, Deflate chops up all parts into blocks to make processing easier. Then it decides in which mode it should encode that block:</p>
<ol>
<li>Store raw stream</li>
<li>Store with a static Huffman encoding</li>
<li>Store with a dynamic Huffman Encoding</li>
</ol>
<p>Now, it still doesn&#8217;t deflate. But lets see.</p>
<p><strong>First mode is raw mode.</strong> In raw mode, Deflate decides that the data is already compressed and it can&#8217;t do a better job. So it just inserts its header to say &#8220;I can&#8217;t do anything with that, this is already compressed, dummy!&#8221;. That&#8217;s why, the next time a coworker shouts at you why you didn&#8217;t ZIP these files, you can shout back: &#8220;D&#8217;oh, mode 1, man!&#8221;.</p>
<p><strong>Second mode is static Huffman mode.</strong> This just means that Deflate decides that the data is standard enough that the standard Huffman Encoding table should be used. For example useful for English text. This way Deflate doesn&#8217;t have to add a table as overhead to the compressed content.</p>
<p><strong>Third is dynamic Huffman mode.</strong> With dynamic Huffman mode, the content gets compressed with a Huffman Encoding, but the encoding table is generated based on the input. So for example, in Finish, the shortest code would probably be &#8220;ö&#8221;, or something like that. In <a href="http://twitter.com/zedshaw" title="Zed Shaw on Twitter">@zedshaw</a>&#8216;s <a href="http://twitter.com/zedshaw/status/237069420281872385" title="Zed Shaw">recent</a> <a href="http://twitter.com/zedshaw/status/237069091368730624" title="Zed Shaw">tweets</a> it could be &#8220;C&#8221;, &#8220;F&#8221;, &#8220;K&#8221;  and &#8220;U&#8221;, who knows!</p>
<p>But that leaves us with two question: Huffman Encoding, wut? And LZ77?</p>
<h3>Huffman Encoding</h3>
<p>Huffman Encoding is something that a guy called Huffman developed while getting a Ph.D from <del datetime="2012-08-21T06:14:29+00:00">MTI</del> MIT. Nothing surprising until now! The paper on it is actually from 1952. Damn it! That&#8217;s double my age! That paper should feel old!</p>
<p>What it basically says: Tally up all individual characters. The most used one gets the shortest code (at 3 bits). Then continue down until you arrived at the end. Actually, Wikipedia does a better job at that than I ever could, so here&#8217;s a picture:<br />
<div id="attachment_241" class="wp-caption alignnone" style="width: 610px"><a href="http://the.codepoet.ch/wp-content/uploads/2012/08/huffman.png"><img src="http://the.codepoet.ch/wp-content/uploads/2012/08/huffman.png" alt="Huffman Coding Tree" title="Huffman Coding Tree" width="1000" height="643" class="size-full wp-image-241" /></a><p class="wp-caption-text">Huffman Coding Tree</p></div><br />
(Source: http://en.wikipedia.org/wiki/File:Huffman_tree_2.svg)</p>
<p>So that one sounded scary, turned out to be really easy!</p>
<h3>So What About That LZ77</h3>
<p>The next part in this tour around compression algorithms leads us right back into history. Can&#8217;t we get something new going in the compression world?</p>
<p>LZ77 was introduced in a paper in, well, 1977 by Lempel and Ziv. LZ77 is a so-called <strong>sliding window compressor</strong>. At least they came up with impressive names back then. Why sliding window? It checks the last 2, 4 or 32 kB for repeating strings. Simple as that.</p>
<p>Should it find one such repeating string, instead of adding that string to the output stream, it puts a back marker in that basically says: If you want to finish reading that sentence, go back to page 32, line 17, and read from character 17 to character 34. That lazy bastard!</p>
<h3>Now I See How They Tie Together</h3>
<p>Well I hope you do! Else I failed and you should shame in the comments. <img src='http://the.codepoet.ch/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Or of course I can try again:</p>
<p>First, Deflate splits up the document into blocks to make working with it easier. Then (lets assume the input isn&#8217;t already compressed) it runs each block through LZ77 to get all the obvious double strings out of the way. Then it sends it through a Huffman Encoding to make it even shorter. </p>
<p>So yes, that&#8217;s deflate.</p>
<h3>What About The Others (BZIP2 Anyone?)</h3>
<p>Not all of them use this simple approach, because simple apparently isn&#8217;t good enough. BZIP2 goes through 9 different stages.</p>
<ol>
<li>Replacing all 4 or more repeating characters with just 4 characters. (There you see, a geek had to make this algorithm for comic books! &#8220;AAAAAAAAAA!&#8221; is so much longer than &#8220;AAA\7!&#8221;) So called Comic Book Encoding&#8230; Unfortunately not, it&#8217;s simply called <a href="http://en.wikipedia.org/wiki/Run-length_encoding" title="Run-length Encoding">Run Length Encoding</a>. How boring!</li>
<li>Next they wheel around the characters. Which basically means a fancy way to sort the same letters after each other so compression is easier. The so called <a href="http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform" title="Burros-Wheeler Transform">Burrows-Wheeler Transform</a>.</li>
<li>Replace all repeating characters with 0, so we have as many 0 as possible. Could also be any other character. The <a href="http://en.wikipedia.org/wiki/Move-to-front_transform" title="Move-to-front Transform">Move-to-Front Transform</a> (Yes, I hate these people too at concerts).</li>
<li>And again, after sorting everything possible, we do a Run-length Encoding.</li>
<li>Then a Huffman Encoding. Don&#8217;t tell me now I have to explain what that is!</li>
<li>Then again we see Huffmaaa\7n! This time with multiple tables per block to make the output even shorter.</li>
<li>8. and 9. Add some binary trickery.</li>
</ol>
<p>So yes, you can use a lot of other things, but it all comes back to making the output even shorter. Surprising, hu?</p>
<h3>So That&#8217;s Compression</h3>
<p>Yes, that&#8217;s compression. Use a bit of special encoding, remove duplicates, and you are halfway there. Of course, this is just for Deflate. We saw that BZIP2 is already a lot more complicated and uses a lot of different tricks to make the output even smaller, but then again, it is also slower!</p>
<p><em>If you liked this post, you should follow me <a href="https://twitter.com/codepoet_ch" title="Twitter">on Twitter</a> and sign up for the <a href="http://feeds.feedburner.com/TheCodePoet" title="RSS feed">RSS feed</a>!</em></p>
<p><small><em>B58P3MMWCDTH</em></small></p>
]]></content:encoded>
			<wfw:commentRss>http://the.codepoet.ch/what-others-know-about-compression-that-you-dont/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.176 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-06-13 16:28:06 -->

<!-- Compression = gzip -->