<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Skilldrick</title>
	
	<link>http://skilldrick.co.uk</link>
	<description>A blog about programming and related stuff by Nick Morgan</description>
	<lastBuildDate>Mon, 05 Mar 2012 04:48:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/skilldrick" /><feedburner:info uri="skilldrick" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>2012</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/pbttFuW_NI4/</link>
		<comments>http://skilldrick.co.uk/2012/01/2012/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 18:31:39 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1452</guid>
		<description><![CDATA[It&#8217;s time for another one of those self-indulgent &#8220;what am I going to do with the next year?&#8221; posts. I can&#8217;t believe it&#8217;s been a whole year since the last one. First off, I&#8217;m going to look at the past year. Finish SICP I definitely progressed with it, but not finished yet. Get a decent [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time for another one of those self-indulgent &#8220;what am I going to do with the next year?&#8221; posts. I can&#8217;t believe it&#8217;s been a whole year since <a href="http://skilldrick.co.uk/2011/01/2011/">the last one</a>.</p>

<p>First off, I&#8217;m going to look at the past year. <strong>Finish SICP</strong> I definitely progressed with it, but not finished yet. <strong>Get a decent online portfolio</strong> As a matter of course I put all my toy projects into <a href="https://github.com/skilldrick">my Github account</a>, but there&#8217;s nothing particularly exciting there. <strong>Keep blogging</strong> I did pretty well with this until October. <strong>Do something good in node.js</strong> I started this, and I&#8217;d like to carry on at some point. I definitely <em>get</em> Node now, but I want to play more. <strong>Learn a functional language</strong> I&#8217;ve started reading/working through the print version of <a href="http://learnyouahaskell.com/">Learn You a Haskell for Great Good</a>, which is brilliant. <strong>Stack Overflow</strong> I kinda neglected my SO profile over the latter half of the year. Oh well.</p>

<p>So, those were the steps I&#8217;d laid out for myself for last year, but they were all in service of my main goal of 2011, which was to</p>

<blockquote>
  <p>establish myself as someone who knows what they’re doing and people will pay to code stuff.</p>
</blockquote>

<p>With this, <a href="http://knowyourmeme.com/memes/everything-went-better-than-expected">everything went better than expected</a>.</p>

<p>In August, two crazy things happened. First, I was approached by a talent scout at Facebook and asked if I was interested. I said hell yes, so they sent me a pre-interview puzzle thing to complete. <a href="http://skilldrick.co.uk/puzzle/">This was my solution</a>. They liked that, so I went on to have three phone screens. After those they rang me up and asked if I&#8217;d like to come out to Palo Alto for an interview! My mind was officially blown at this point.</p>

<p>Things got even crazier when I received an email the night before I was due to fly out to see Facebook, from a recruiter at Twitter. I&#8217;d been referred by JavaScript guru <a href="http://javascriptweblog.wordpress.com/">Angus Croll</a>, to whom I&#8217;m eternally grateful.</p>

<p>Anyway, I went out to Palo Alto, visited San Francisco, had my interviews at Facebook HQ, and flew home. After I got back I had my phone screens with Twitter, followed by another offer of an interview. I found out on my way to the interview in San Francisco that I didn&#8217;t get the Facebook job &#8211; they were looking for a JS/CSS developer, and my CSS skills aren&#8217;t top notch. Also, I think they picked up on my general meh-ness about the actual Facebook product.</p>

<p>So, another trip to San Francisco (which cemented my growing suspicion that SF is <em>the</em> place for me &#8211; such an awesome city), more interviews, and then, a few days later, a job offer! So, now I&#8217;m living in San Francisco, working on what is probably my favourite thing on the internet, with legends. And it&#8217;s awesome.</p>

<p>So, anyway, 2012.</p>

<h3>2012</h3>

<p>I don&#8217;t want to rest on my laurels. There&#8217;s still so much more I want to learn.</p>

<ul>
<li><p>Finish Learn You a Haskell for Great Good</p></li>
<li><p>Finish SICP</p></li>
<li><p>Write something decent in Node (still)</p></li>
<li><p>Read more of the books on the first page of <a href="http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read">this legendary Stack Overflow question</a> (I&#8217;m at about 30% so far)</p></li>
<li><p>Branch away from JavaScript a bit, maybe with Scala</p></li>
<li><p>Start blogging again (this is my first post since October &#8211; whoops!)</p></li>
</ul>

<p>Anyway, here&#8217;s to 2012!</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/pbttFuW_NI4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2012/01/2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2012/01/2012/</feedburner:origLink></item>
		<item>
		<title>Three years on</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/gtid7n9ej8A/</link>
		<comments>http://skilldrick.co.uk/2011/10/three-years-on/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 13:12:27 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1435</guid>
		<description><![CDATA[On 23 October 2008, on holiday in Devon with my girlfriend, I read an article in the Guardian&#8217;s Technology section about the UK games industry. From that point on I was certain that I wanted to become a developer of some kind. I wrote more about this in Finding your passion. It&#8217;s now three years [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://skilldrick.co.uk/wp-content/uploads/2011/10/portal_3_years.jpg"><img src="http://skilldrick.co.uk/wp-content/uploads/2011/10/portal_3_years-300x240.jpg" alt="Portal cake with three candles" title="Portal three years cake" width="300" height="240" class="alignleft size-medium wp-image-1445" /></a> On 23 October 2008, on holiday in Devon with my girlfriend, I read <a href="http://www.guardian.co.uk/technology/2008/oct/23/games-industry">an article in the Guardian&#8217;s Technology section about the UK games industry</a>. From that point on I was certain that I wanted to become a developer of some kind. I wrote more about this in <a href="http://skilldrick.co.uk/2010/09/finding-your-passion/">Finding your passion</a>.</p>

<p>It&#8217;s now three years and a day since then, and I&#8217;m winding down at my current job in preparation for a move to San Francisco, as I somehow managed to land myself a job at <a href="http://twitter.com">Twitter</a> as a junior front-end engineer. <a href="http://www.youtube.com/watch?v=alQjtnWQlAE">Which was nice</a>. I&#8217;m unbelievably chuffed that I&#8217;ve managed to get to where I am today, and I hope the next three years will be just as exciting as the last :)</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/gtid7n9ej8A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/10/three-years-on/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/10/three-years-on/</feedburner:origLink></item>
		<item>
		<title>Closures vs Objects: FIGHT</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/2itkjtlbe-U/</link>
		<comments>http://skilldrick.co.uk/2011/09/closures-vs-objects-fight/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 02:50:14 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1420</guid>
		<description><![CDATA[In JavaScript, there are two main patterns for creating objects with state - plain JavaScript objects and closures. In this post I&#8217;m going to highlight the similarites and consider the pros and cons of the two approaches. An object is an entity with state and methods to access/modify that state. There are two main approaches [...]]]></description>
			<content:encoded><![CDATA[<p>In JavaScript, there are two main patterns for creating objects with state -
plain JavaScript objects and closures. In this post I&#8217;m going to highlight the
similarites and consider the pros and cons of the two approaches.</p>

<p>An object is an entity with state and methods to access/modify that state. There
are two main approaches to this, which I&#8217;ll be calling &#8220;objects&#8221; and &#8220;closures&#8221;. For the rest of this post I&#8217;ll only use the word object to refer to plain JavaScript objects, in an attempt to avoid confusion.</p>

<pre><code>//object
function Person(name) {
  this.name = name;
}

Person.prototype.sayHi = function () {
  console.log('Hi there, my name is ' + this.name);
};


//closure
function person(name) {
  //using object literal but state held in closure, not in object
  return {
    sayHi: function () {
      console.log('Hi there, my name is ' + name);
    }
  };
}
</code></pre>

<p>Now, there are infinite variations to the above. I&#8217;m using native JavaScript
constructors in the object version, but it doesn&#8217;t have to be that way. For
example, I could make a function that returns an object, without having to use
<code>new</code>:</p>

<pre><code>//alternative object
function person(name) {
  return {
    name: name,
    sayHi: function () {
      console.log('Hi there, my name is ' + this.name);
    }
  };
}
</code></pre>

<p>Alternatively, I could come up with a more convoluted example that caches the
<code>sayHi</code> method so it&#8217;s shared between instances.</p>

<p>The point is, using objects, state is shared through <code>this</code>. Whenever you call a
method on an object, e.g.</p>

<pre><code>var dave = new Person('Dave');
dave.sayHi();
</code></pre>

<p><code>this</code> within the method will be equal to the object it was called upon. When using
closures however, state is shared through the lexical scope. This highlights the
first key difference between objects and closures: access to the internal state.</p>

<p>There are three ways to mutate the internal state of an object in JavaScript.
I&#8217;ll illustrate with some examples:</p>

<pre><code>//Obtain a reference to the object and assign new properties
function changeName(object, newName) {
  object.name = newName;
}

changeName(dave, 'Bob');


//Attach a function to the object and call it as a method on the object
function changeName(newName) {
  this.name = newName;
}

dave.changeName = changeName;
dave.changeName('Bob');


//call/apply a function with the object as the context
changeName.call(dave, 'Bob');
</code></pre>

<p>With closures, on the other hand, there is only one way to mutate the internal
state &#8211; be inside the scope of the closure:</p>

<pre><code>function person(name) {
  //to change `name`, you *must* be defined somewhere inside this function

  return {
    sayHi: function () {
      console.log('Hi there, my name is ' + name);
    }
  };
}
</code></pre>

<p>This can be a blessing and a curse. The advantage of objects over closures is
that you&#8217;re not limited in the functionality you can add to an object by
location in the source code. If you decide that you need to add more
functionality to an object, you can do this at any point in your codebase. With
a closure the only way to add functionality (with access to the internal state)
is to define it somewhere inside the function that creates the closure.</p>

<p>The advantage of closures over objects is the same as the disadvantage but from
the perspective of third party code. With an object, anybody can add or change
functionality on your object, and access its internal state. With a closure, the
internal state is private &#8211; it can&#8217;t be accessed from outside the closure
without the use of accessor functions.</p>

<p>Another advantage that objects have is in terms of memory usage. With a closure,
by definition, for a function to have access to the internal state it must be
<em>defined</em> inside the closure. That means that each new closure created <em>must</em>
have its own version of the function. Objects on the other hand have no such
limitation. A function that reads from and writes to <code>this</code> need only be defined
once &#8211; it can then be added to any object, either shared via the prototype
system or through other means.</p>

<p>An advantage that closures have is that you don&#8217;t need to keep track of <code>this</code>.
With closures it&#8217;s simple &#8211; you&#8217;re either in the correct scope or you&#8217;re not.
With objects, if you&#8217;re in a method called on an object then <code>this</code> is that
object, but if the method gets detached from its object (e.g. when passed as an
argument) it loses its binding, or if you have a nested function inside the
method. Then you need to start messing about with <code>call</code> and <code>apply</code>, and <code>bind</code>
and other fun stuff.</p>

<p>So, when should you use objects and when closures? If you&#8217;re making hundreds of
object-type things then they should probably be objects. If there are only a few
and you have security concerns then closures are a better bet.</p>

<p>Privacy isn&#8217;t just a security issue &#8211; it&#8217;s useful for creating a clean
separation between the public API and the private implementation. Some
developers would say that JavaScript doesn&#8217;t provide privacy, so you should get
used to writing objects with everything public, and tell users of your code to
just not touch certain properties (for example those with a leading underscore).
I think it&#8217;s useful being able to enforce privacy &#8211; it makes sure that no-one
will ever write code that&#8217;s reliant on an implementation detail of yours.</p>

<p>Because of this, I tend to favour closures, falling back to objects when memory
becomes a concern, but that&#8217;s largely a personal preference.</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/2itkjtlbe-U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/09/closures-vs-objects-fight/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/09/closures-vs-objects-fight/</feedburner:origLink></item>
		<item>
		<title>Understanding typeof, instanceof and constructor in JavaScript</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/DZuxsNt_tQw/</link>
		<comments>http://skilldrick.co.uk/2011/09/understanding-typeof-instanceof-and-constructor-in-javascript/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 19:10:39 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1398</guid>
		<description><![CDATA[They say in JavaScript &#8220;everything is an object&#8221;. They&#8217;re wrong. Some types in JavaScript are so-called &#8220;primitive types&#8221;, and they don&#8217;t act like objects. These types are: Undefined Null Boolean Number String The confusion comes from the fact that the boolean, number and string types can be treated like objects in a limited way. For [...]]]></description>
			<content:encoded><![CDATA[<p>They say in JavaScript &#8220;everything is an object&#8221;. They&#8217;re wrong. Some types in JavaScript are so-called &#8220;primitive types&#8221;, and they don&#8217;t act like objects. These types are:</p>

<ul>
<li>Undefined</li>
<li>Null</li>
<li>Boolean</li>
<li>Number</li>
<li>String</li>
</ul>

<p>The confusion comes from the fact that the boolean, number and string types can be treated like objects in a limited way. For example, the expression <code>"I'm no object".length</code> returns the value <code>13</code>. This happens because when you attempt to access properties or methods on a primitive value, JavaScript instantiates a wrapper object temporarily, just so you can access its methods. &#8216;Cause JavaScript&#8217;s nice like that. I&#8217;m not going to go into more details here, but Angus Croll wrote about <a href="http://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/">The Secret Life of JavaScript Primitives</a>, so that would be a good place to learn more.</p>

<h3><code>typeof</code></h3>

<p><code>typeof</code> is a unary operator, just like the <code>!</code> operator. It returns a string representing the type of its operand. Here are some examples:</p>

<pre><code>typeof 3; // returns "number"
typeof 'blah'; //returns "string"
typeof {}; //returns "object"
typeof []; //returns "object"
typeof function () {}; //returns "function"
</code></pre>

<p><code>typeof</code> has its idiosyncrasies. For example, <code>typeof null</code> returns <code>"object"</code>, and <code>typeof /[a-z]/</code> returns <code>"function"</code>. Again, Angus Croll has <a href="http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/">written more on this subject</a> than I have space for here.</p>

<p>So, basically <code>typeof</code> is used for telling apart the different primitive types (as long as you don&#8217;t care about <code>null</code>). It&#8217;s no use for telling different types of object apart though &#8211; for most objects <code>typeof</code> will return <code>"object"</code>.</p>

<h3><code>constructor</code></h3>

<p><code>constructor</code> is a property available on all objects&#8217; prototypes, and it is a reference to the constructor function used to create the object. So, <code>({}).constructor</code> returns the <code>Object</code> constructor function (the parentheses are needed to clarify a syntactic ambiguity) and <code>[].constructor</code> returns the <code>Array</code> constructor function. Likewise, it will return your custom constructor function:</p>

<pre><code>function Person(name) {
  this.name = name;
}

var dave = new Person('Dave');
dave.constructor === Person; //true
</code></pre>

<p>Remember that unlike the <code>typeof</code> operator, <code>constructor</code> returns a reference to the actual function. Another gotcha: because <code>constructor</code> is part of the prototype, if you reassign the prototype to a constructor function, e.g. <code>Person.prototype = {};</code>, you&#8217;ll lose the <code>constructor</code> property.</p>

<h3><code>instanceof</code></h3>

<p><code>instanceof</code> is a binary operator &#8211; its syntax is <code>instance instanceof Constructor</code>. So, to continue the above example:</p>

<pre><code>dave instanceof Person; //true
</code></pre>

<p>The difference between <code>instanceof</code> and the <code>constructor</code> property (apart from the obvious syntactic difference) is that <code>instanceof</code> inspects the object&#8217;s prototype chain. So, going back to our friend <code>dave</code> again:</p>

<pre><code>dave instanceof Object; //true
</code></pre>

<p>This is because <code>Person.prototype</code> is an object, so <code>Object</code> is in <code>dave</code>&#8216;s prototype chain, therefore <code>dave</code> is an instance of <code>Object</code>.</p>

<h3>Wrap-up</h3>

<p>So, if you&#8217;re dealing with primitive objects, use <code>typeof</code> to distinguish them. Because <code>typeof</code> returns <code>"function"</code> for functions, it can also be useful for checking if an object member or a function argument is a function. If you&#8217;re working out the constructor of an object, use its <code>constructor</code> property. And if you&#8217;re dealing with lengthy inheritance chains, and you want to find out whether an object inherits from a certain constructor, use <code>instanceof</code>.</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/DZuxsNt_tQw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/09/understanding-typeof-instanceof-and-constructor-in-javascript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/09/understanding-typeof-instanceof-and-constructor-in-javascript/</feedburner:origLink></item>
		<item>
		<title>If the only tool you have is Java, everything looks like a class</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/a83jGfLo3c0/</link>
		<comments>http://skilldrick.co.uk/2011/09/if-the-only-tool-you-have-is-java-everything-looks-like-a-class/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 19:30:28 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1331</guid>
		<description><![CDATA[&#60;rant&#62; There&#8217;s a common phrase used within software development: If the only tool you have is a hammer, everything looks like a nail It exists in various forms, but the original source is the Law of the instrument, or Maslow&#8217;s Hammer. I started thinking about this while reading through Head First Design Patterns today (a [...]]]></description>
			<content:encoded><![CDATA[<p><code>&lt;rant&gt;</code></p>

<p>There&#8217;s a common phrase used within software development:</p>

<blockquote>
  <p>If the only tool you have is a hammer, everything looks like a nail</p>
</blockquote>

<p>It exists in various forms, but the original source is the <a href="http://en.wikipedia.org/wiki/Law_of_the_instrument">Law of the instrument, or Maslow&#8217;s Hammer</a>.</p>

<p>I started thinking about this while reading through <a href="http://www.amazon.co.uk/gp/product/0596007124/ref=as_li_ss_tl?ie=UTF8&amp;tag=skilldrick-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=0596007124">Head First Design Patterns</a> today (a bizarre book, not quite sure yet whether I can recommend it).</p>

<p>What struck me was that the solution to every design problem in Java revolves around writing a new class. It just astounds me that there&#8217;s basically <strong>no other means of abstraction</strong> in the language. I&#8217;ve got so used to languages with lambdas, for example, that the idea of creating a <code>LightOnCommand</code> class that implements the <code>Command</code> interface to encapsulate the act of turning a light on feels like such a waste of brainpower, bytes, and sanity. I&#8217;d hate to have my expressivity limited so heavily.</p>

<p><code>&lt;/rant&gt;</code></p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/a83jGfLo3c0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/09/if-the-only-tool-you-have-is-java-everything-looks-like-a-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/09/if-the-only-tool-you-have-is-java-everything-looks-like-a-class/</feedburner:origLink></item>
		<item>
		<title>Making inheritable objects in JavaScript without objects</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/Z6Sjq4Z8lCE/</link>
		<comments>http://skilldrick.co.uk/2011/08/making-inheritable-objects-in-javascript-without-objects/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 11:00:43 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1370</guid>
		<description><![CDATA[For a long time I&#8217;ve found the subject of the equivalence of closures and objects fascinating. The problem with creating objects using closures is that there isn&#8217;t a way to do inheritance (although you should always favour composition over inheritance anyway!). I thought it would be fun to see if it was possible to implement [...]]]></description>
			<content:encoded><![CDATA[<p>For a long time <a href="http://skilldrick.co.uk/2011/02/zen-and-the-art-of-statefulness/">I&#8217;ve found the subject of the equivalence of closures and objects fascinating</a>. The problem with creating objects using closures is that there isn&#8217;t a way to do inheritance (although you should always favour composition over inheritance anyway!). I thought it would be fun to see if it was possible to implement an object system in JavaScript with inheritance, without using any plain JavaScript objects.</p>

<p>To do this, I set myself two rules:</p>

<ol>
<li>Don&#8217;t create any plain old JavaScript objects (functions, arrays etc. are allowed)</li>
<li>Don&#8217;t use the dot operator.</li>
</ol>

<p>For that to work, I wrote some wrappers for the JavaScript methods I would need, like <code>Array.prototype.slice.call</code> (this becomes <code>callSlice</code>). Apart from those wrappers, there are no dots used anywhere. To dispatch methods based on a string name, instead of using an object (which would have been much easier but would have missed the point of the exercise) I&#8217;m passing around functions which dispatch methods using conditional constructs (<code>if</code>, <code>else</code> and <code>switch</code>).</p>

<h3>Introducing <code>funcobj</code>!!!</h3>

<p>Yeah I know, it&#8217;s a rubbish name. Anyway, the <a href="https://github.com/skilldrick/funcobj">Github repo is here</a> and I&#8217;ve got a <a href="http://jsfiddle.net/skilldrick/rquCU/">running example on JsFiddle here</a>.</p>

<p>Here&#8217;s some examples of how it works:</p>

<pre><code>//call the doSomething method on myObject, with the argument 500
myObject('doSomething')(500);

//call the doSomethingElse method on the object's super object
myObject('doSomethingElse', true)();
</code></pre>

<p>To make an object like this, call <code>objMaker</code> with a function that defines and dispatches methods:</p>

<iframe style="width: 95%; height: 300px" src="http://jsfiddle.net/skilldrick/Qtncr/3/embedded/"></iframe>

<p>To inherit, pass a third argument to <code>objMaker</code>, which is the object to delegate to if the new object does not contain the called method (or: if the new object cannot respond to the message). Have a look at this long example in JsFiddle to see how that works:</p>

<iframe style="width: 95%; height: 500px" src="http://jsfiddle.net/skilldrick/rquCU/embedded/"></iframe>

<h3>This is stupid</h3>

<p>Yeah, I know. It&#8217;s not meant to be used for anything. It&#8217;s probably hopelessly inefficient, and the syntax leaves a lot to be desired. But it does <em>work</em>, which was what I was going for. It might be a stupid idea, but it demonstrates the power of JavaScript closures. It also gives the possibility of dynamically responding to unknown messages, like in Ruby&#8217;s <code>method_missing</code>.</p>

<p>This is what <code>objMaker</code> looks like. There&#8217;s some fairly gnarly code in there, but if you can get your head around what&#8217;s doing what, you&#8217;ll hopefully learn something new about JavaScript!</p>

<iframe style="width: 95%; height: 600px" src="http://jsfiddle.net/skilldrick/vXZmN/embedded/"></iframe>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/Z6Sjq4Z8lCE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/08/making-inheritable-objects-in-javascript-without-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/08/making-inheritable-objects-in-javascript-without-objects/</feedburner:origLink></item>
		<item>
		<title>Podcasts I listen to</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/LunrnhhT4WE/</link>
		<comments>http://skilldrick.co.uk/2011/08/podcasts-i-listen-to/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 16:41:57 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[podcasts]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1328</guid>
		<description><![CDATA[I listen to a lot of podcasts. Any time I can find to listen to them I do &#8211; when I&#8217;m cooking, eating, washing up, walking to work, brushing my teeth, going shopping, running &#8230; It&#8217;s not always easy to find the best podcasts, so I thought it would be helpful to list them here, [...]]]></description>
			<content:encoded><![CDATA[<p>I listen to a lot of podcasts. Any time I can find to listen to them I do &#8211; when I&#8217;m cooking, eating, washing up, walking to work, brushing my teeth, going shopping, running &#8230;</p>

<p>It&#8217;s not always easy to find the best podcasts, so I thought it would be helpful to list them here, along with a quick description, and some selected episodes where appropriate.</p>

<p>This list is in rough order of preference. I&#8217;m not saying the ones at the top are objectively better; just that I enjoy them more.</p>

<h3><a href="http://drunkandretired.com/thepodcast/">Drunk and Retired</a></h3>

<p>One of my favourite podcasts. Cot&eacute; and Charles Lowell just chatting, usually about software, but whatever else they fancy as well. Always funny, often enlightening.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://drunkandretired.com/2008/12/11/episode-142-the-barbed-wire-on-the-nuts/">#142: The Barbed Wire on the Nuts</a></li>
<li><a href="http://drunkandretired.com/2010/03/04/episode165/">#165: Cloud Closures and Server-side JavaScript Injection</a></li>
<li><a href="http://drunkandretired.com/2010/04/29/happy-birthday-nick-morgan-drunkandretired-com-166/">#166: Happy birthday Nick Morgan!</a> &#8211; my girlfriend emailed Cot&eacute; and asked them to wish me happy birthday, so they recorded an episode! </li>
<li><a href="http://drunkandretired.com/2011/01/07/episode170/">#170: Of cigars, JavaScript, VIM, Christmas gifts, and things that don’t pan out</a></li>
</ul>

<h3><a href="http://5by5.tv/b2w">Back to Work</a></h3>

<p>Merlin Mann (of 43 folders and Inbox Zero) and Dan Benjamin (of the Ruby world) chatting about creating, productivity, Buddhism and life. Hilarious and amazing.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://5by5.tv/b2w/8">#8: Little Velvet Hands</a></li>
<li><a href="http://5by5.tv/b2w/10">#10: At Last the &#8216;Inspiration&#8217; Show</a></li>
<li><a href="http://5by5.tv/b2w/12">#12: Chewie&#8217;s Medal is Not Canonical</a></li>
<li><a href="http://5by5.tv/b2w/20">#20: Muscle of Failure</a></li>
</ul>

<h3><a href="http://thisdeveloperslife.com/">This Developer&#8217;s Life</a></h3>

<p>An excellent podcast, unlike any other I listen to. Every episode follows a theme, with storytelling interviews around that theme, backed by good music. Can&#8217;t get enough of it.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://thisdeveloperslife.com/post/1-0-5-homerun">1.0.5: Homerun</a></li>
<li><a href="http://thisdeveloperslife.com/post/1-0-6-abstraction">1.0.6: Abstraction</a> &#8211; this one&#8217;s a real gem, discussing just how many layers of abstraction you need to know about. Very relevant to my post on <a href="http://skilldrick.co.uk/2011/04/magic-in-software-development/">magic</a>.</li>
<li><a href="http://thisdeveloperslife.com/post/1-0-7-audacity">1.0.7: Audacity</a></li>
<li><a href="http://thisdeveloperslife.com/post/1-1-2-drive">1.1.2: Drive</a></li>
</ul>

<h3><a href="http://rubyrogues.com/">Ruby Rogues</a></h3>

<p>This is a fairly new Ruby discussion podcast. It features all your favourite celebrity Ruby developers, like James Edward Gray II, Aaron Patterson, Peter Cooper, Avdi Grimm, Gregory Brown and more. It&#8217;s fascinating to hear these at-the-top-of-their-game developers talking about why they do things in a certain way. Each episode is a discussion around a specific topic.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://rubyrogues.com/databases-sql-nosql/">Databases, SQL, &amp; NoSQL</a></li>
<li><a href="http://rubyrogues.com/conferences-and-user-groups/">Conferences and User Groups</a></li>
<li><a href="http://rubyrogues.com/debugging-in-ruby/">Debugging in Ruby</a> &#8211; apparently even top Ruby developers debug using <code>puts</code>. That&#8217;s good to know :)</li>
<li><a href="http://rubyrogues.com/what-makes-beautiful-code/">What Makes Beautiful Code</a></li>
</ul>

<h3><a href="http://www.se-radio.net/">Software Engineering Radio</a></h3>

<p>&#8220;The Podcast for Professional Software Developers&#8221;. An excellent, quite formal podcast about software engineering topics. It has a bit of a Java, patterns and model-driven architecture bias, but it&#8217;s second-to-none in terms of serious software engineering/development content. Most of the interviewers/editors are German, so it&#8217;s got a much more &#8216;European&#8217; view on things (you may not think there&#8217;s any difference, but I think there is).</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://www.se-radio.net/2009/07/episode-140-newspeak-and-pluggable-types-with-gilad-bracha/">#140: Newspeak and Pluggable Types with Gilad Bracha</a></li>
<li><a href="http://www.se-radio.net/2009/07/episode-141-second-life-and-mono-with-jim-purbrick/">#141: Second Life and Mono with Jim Purbrick</a></li>
<li><a href="http://www.se-radio.net/2009/11/episode-148-software-archaeology-with-dave-thomas/">#148: Software Archaeology with Dave Thomas</a></li>
<li><a href="http://www.se-radio.net/2009/11/episode-150-software-craftsmanship-with-bob-martin/">#150: Software Craftsmanship with Bob Martin</a></li>
<li><a href="http://www.se-radio.net/2010/01/episode-154-ola-bini-on-ioke/">#154: Ola Bini on Ioke</a></li>
<li><a href="http://www.se-radio.net/2010/03/episode-158-rich-hickey-on-clojure/">#158: Rich Hickey on Clojure</a></li>
<li><a href="http://www.se-radio.net/2010/09/episode-167-the-history-of-junit-and-the-future-of-testing-with-kent-beck/">#167: The History of JUnit and the Future of Testing with Kent Beck</a> &#8211; it was nice to hear on this episode that <em>even Kent Beck</em> doesn&#8217;t test drive when he&#8217;s doing highly exploratory work.</li>
</ul>

<h3><a href="http://pragprog.com/podcasts">The Pragmatic Podcast</a></h3>

<p>This is usually interviews with Prag Prog authors, so can often come across a bit like a sales pitch, but occasionally there&#8217;s a real gem.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://pragprog.com/podcasts/show/21">#21: Keir Thomas on Ubuntu Kung Fu</a></li>
<li><a href="http://pragprog.com/podcasts/show/32">#32: Robert Martin interview</a> &#8211; that&#8217;s Uncle Bob to you.</li>
<li><a href="http://pragprog.com/podcasts/show/36">#36: Ward Cunningham Interview</a></li>
</ul>

<h3><a href="http://www.aminutewithbrendan.com/">A Minute With Brendan</a></h3>

<p>Brendan Eich, JavaScript&#8217;s Daddy, talking about the present and future of JavaScript. Hear about JavaScript from the horse&#8217;s mouth.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://www.aminutewithbrendan.com/pages/20100830">JS Type Inference and Static Analysis</a></li>
<li><a href="http://www.aminutewithbrendan.com/pages/20110131">The Harmony Of My Dreams</a></li>
<li><a href="http://www.aminutewithbrendan.com/pages/20110216">Closure Versus Prototypal Pattern Deathmatch</a></li>
</ul>

<h3><a href="http://creativecodingpodcast.com/">The Creative Coding Podcast</a></h3>

<p>Fairly informal chats mostly about game development/general creative coding, with the odd interview thrown in.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://creativecodingpodcast.com/?p=1">#1: Web games, HTML5 and Jangaroo</a></li>
<li><a href="http://creativecodingpodcast.com/?p=73">#5: Game Design and Conference Etiquette with Ryan Henson Creighton</a></li>
<li><a href="http://creativecodingpodcast.com/?p=87">#7: openFrameworks and Open Source with Pete Hellicar and Joel Gethin Lewis</a></li>
<li><a href="http://creativecodingpodcast.com/?p=92">#8: Rome, Chrome and Angry Birds</a></li>
</ul>

<h3><a href="http://itc.conversationsnetwork.org/">IT Conversations</a></h3>

<p>I try to listen to every IT Conversations podcast, but there are <em>a lot</em> of them. It&#8217;s always interesting stuff, and it covers a wide range of topics in the tech world, from programming to biotech to robotics, etc. IT Conversations is actually a collection of other podcasts and conference recordings.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://itc.conversationsnetwork.org/shows/detail4601.html">Where 2.0 Conference: Michael Arrington and Tim O&#8217;Reilly &#8211; Location and Points of Control</a></li>
<li><a href="http://itc.conversationsnetwork.org/shows/detail4773.html">Web 2.0 Conference: Robin Li &#8211; Chinese Web Search Entrepreneurship</a> &#8211; from the founder of Baidu.</li>
<li><a href="http://itc.conversationsnetwork.org/shows/detail4911.html">Technometria: Pete Kruckenberg &#8211; Apache/Perl vs. node.js</a></li>
<li><a href="http://itc.conversationsnetwork.org/shows/detail4841.html">Tech Nation: Mark Stevenson &#8211; What&#8217;s Next?</a></li>
</ul>

<h3><a href="http://www.redmonk.com/cote/topic/podcasts/makeall/">Make All</a></h3>

<p>Cot&eacute; from Drunk and Retired, with his industry analyst hat on, interviewing programming people.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://www.redmonk.com/cote/2011/01/27/makeall014/">#14: Code Reviewing and Tooling the RIA world with Chris Gross</a></li>
<li><a href="http://www.redmonk.com/cote/2011/03/30/makeall015/">#15: What’s in your stack with Serge Knystautas, PrestoSports</a></li>
<li><a href="http://www.redmonk.com/cote/2011/04/01/applets-to-ajax-to-apps-with-stu-stern-of-gorilla-logic-make-all-016/">#16: Applets to Ajax to Apps, with Stu Stern of Gorilla Logic</a></li>
<li><a href="http://www.redmonk.com/cote/2011/04/19/makeall017/">#17: Search as middle-ware at att.com, with Shantanu Deo</a></li>
</ul>

<h3><a href="http://explicitweb.co.uk/">ExplicitWeb</a></h3>

<p>Three UK-based web developers talking about web development issues. It&#8217;s called ExplicitWeb because it has an Explicit rating on iTunes &#8211; be warned.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://explicitweb.co.uk/post/468973232/episode-2-html5-special-direct-link-on-this">#1.2: HTML5 Special</a> &#8211; An introduction to HTML 5 and how to start using it; HTML5 Canvas &#8211; the future of graphics on the internet; HTML5 vs Flash &#8211; Fight!</li>
<li><a href="http://explicitweb.co.uk/post/653783325/episode-6-future-of-web-design-psycho-babble">#1.6: Future of Web Design, Psycho-babble for the Soul, and Arduino</a> &#8211; Future of Web Design: A review; Psycho-babble for the soul: understanding the service provider &#8211; client relationship; Arduino: Making the Web Physical</li>
<li><a href="http://explicitweb.co.uk/post/1392304241/season-2-episode-1-direct-link-staying-ahead">#2.1: Staying ahead of the game</a></li>
<li><a href="http://explicitweb.co.uk/post/1575380286/season-2-episode-2-direct-link-chris-spooner">#2.2: Chris Spooner, Jack Osborne, and Gavin Strange </a></li>
</ul>

<h3><a href="http://javascriptshow.com/">The JavaScript Show</a></h3>

<p>Jason Seifer and Peter Cooper with a slightly irreverent round-up of JavaScript news.</p>

<h3><a href="http://rubyshow.com/">The Ruby Show</a></h3>

<p>The Ruby equivalent of The JavaScript show.</p>

<h3><a href="http://www.webdevradio.com/">Web Dev Radio</a></h3>

<p>A podcast about web development from Michael Kimsal. Partly an interview show with various web developers, partly Michael just talking about stuff.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://www.webdevradio.com/index.php?id=107">#81: indieconf, client-side image resizing, moral dilemmas</a></li>
<li><a href="http://www.webdevradio.com/index.php?id=110">#84: Travis Swicegood</a> &#8211; the author of Pragmatic Version Control Using Git.</li>
</ul>

<h3><a href="http://basementcoders.com/">The Basement Coders</a></h3>

<p>Informal, Java-heavy discussion about software development. Don&#8217;t let the Java put you off though &#8211; I&#8217;ve never written a line of Java, but I still find it interesting.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://basementcoders.com/2011/02/episode-32-benefit-of-dynamic-typing-ps3-jailbreak-verizon-iphone/">#32: Benefit of Dynamic Typing, PS3 Jailbreak, Verizon iPhone</a></li>
<li><a href="http://basementcoders.com/2011/02/episode-33-java-is-a-dead-end-from-stockholms-jfokus-conference/">#33: Java is a Dead End from Stockholm&#8217;s Jfokus Conference</a></li>
<li><a href="http://basementcoders.com/2011/03/episode-34-interview-with-duckduckgos-gabriel-weinberg/">#34: Interview with DuckDuckGo&#8217;s Gabriel Weinberg</a></li>
<li><a href="http://basementcoders.com/2011/05/episode-36-software-engineer-vs-gardener-and-are-net-programmers-really-inferior/">#36: Software Engineer vs Gardener and are .NET programmers really Inferior?</a></li>
</ul>

<h3><a href="http://5by5.tv/pipeline">The Pipeline</a></h3>

<p>Dan Benjamin interviewing various trendy tech people.</p>

<h5>Selected episodes</h5>

<ul>
<li><a href="http://5by5.tv/pipeline/17">#17: Alex Payne</a> &#8211; leaving Twitter to start BankSimple.</li>
<li><a href="http://5by5.tv/pipeline/26">#26: MC Frontalot</a></li>
<li><a href="http://5by5.tv/pipeline/42">#42: Anders Hejdenberg</a> &#8211; doing what you love.</li>
<li><a href="http://5by5.tv/pipeline/43">#43: Mike Monteiro</a></li>
<li><a href="http://5by5.tv/pipeline/44">#44: Jesse Jacobs</a> &#8211; a brilliant story about leaving the SF tech scene to start a fine tea lounge.</li>
<li><a href="http://5by5.tv/pipeline/50">#50: Ze Frank</a></li>
<li><a href="http://5by5.tv/pipeline/54">#54: Ethan Marcotte</a></li>
</ul>

<h3><a href="http://thechangelog.com/">The Changelog</a></h3>

<p>&#8220;Open Source moves fast. Keep up.&#8221; New and cool stuff in open source.</p>

<h3><a href="http://www.guardian.co.uk/technology/series/techweekly">Tech Weekly</a></h3>

<p>The Guardian&#8217;s tech podcast, presented by Aleks Krotoski. A wide-ranging look at the tech industry.</p>

<hr />

<p>There&#8217;s a lot here, but I can just about keep up. I never listen to podcasts when I&#8217;m not doing something else, so I don&#8217;t see it as a waste of time. In fact, I almost see it as a waste of time when I&#8217;m doing something mindless (like washing up, running etc.) and I&#8217;m <em>not</em> listening to podcasts. Maybe that&#8217;s a bit obsessive, but I think it&#8217;s a great way of learning masses of new stuff for free. Anyway, I hope you find something interesting in here. Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/LunrnhhT4WE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/08/podcasts-i-listen-to/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/08/podcasts-i-listen-to/</feedburner:origLink></item>
		<item>
		<title>Understanding the Ruby object model</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/00LrVLusEug/</link>
		<comments>http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 11:50:56 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1288</guid>
		<description><![CDATA[Not a very long time ago, I found the Ruby object model very confusing. Part of my problem was that I was trying to model the relationships as a directed acyclic graph, whereas in reality, the model contains cycles. I&#8217;m guessing that others have found this confusing, so I&#8217;m going attempt to explain how it [...]]]></description>
			<content:encoded><![CDATA[<p>Not a very long time ago, I found the Ruby object model <a href="http://skilldrick.co.uk/2010/11/why-classes-are-confusing-in-ruby/">very confusing</a>. Part of my problem was that I was trying to model the relationships as a <a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph">directed acyclic graph</a>, whereas in reality, the model contains cycles. I&#8217;m guessing that others have found this confusing, so I&#8217;m going attempt to explain how it works in a way that my past self would have understood! Note that this is a basic explanation, and glosses over higher-level concepts like the eigenclass/metaclass (I&#8217;ll leave that for a later post).</p>

<h3>Different <em>things</em> in Ruby</h3>

<p>There are just a few things that exist in Ruby that you need to understand to really grok its object model.</p>

<h4>Objects</h4>

<p><em>Everything is an object</em>. You can&#8217;t get very far into Ruby-land without hearing that phrase. It&#8217;s true though, everything in Ruby is an object. The interesting thing is how those objects are linked together and classified.</p>

<h4>Classes</h4>

<p>All objects are instances of a class. There are three special classes that are the key to understanding the Ruby object model: <code>Object</code>, <code>Module</code> and <code>Class</code>. More on them later.</p>

<h3>Relations</h3>

<p>Like I said before, it&#8217;s the links between the different objects that explain how the Ruby object model works.</p>

<h4>Instance of/class</h4>

<p>All objects have a class &#8211; they are <em>instances of</em> that class. That means that whenever you have a class, you can call <code>.new</code> on it to get an instance. And whenever you have an instance, you can call <code>.class</code> to get its class. This seems obvious, but it can get confusing when you remember that <em>everything is an object</em>, and so <em>everything is an instance of a class</em>.</p>

<h4>Superclass/subclass</h4>

<p>All classes have a link to a superclass. If you don&#8217;t explicitly inherit from another class in a class definition, that class will inherit directly from the <code>Object</code> class.</p>

<h3>How they fit together</h3>

<p>Here&#8217;s some code to play with:</p>

<pre><code>class Animal
end

class Dog &lt; Animal
end

fido = Dog.new
</code></pre>

<p>Once you know what the pieces and the relations are, it&#8217;s fairly straightforward to piece them together. All classes are an instance of <code>Class</code>. That includes the class <code>Class</code>, so there&#8217;s a circular relation there (which isn&#8217;t a problem).</p>

<p><code>Dog</code> will basically have the following inheritance hierarchy (the superclass links):</p>

<pre><code>Dog &lt; Animal &lt; Object &lt; BasicObject
</code></pre>

<p>(Ruby 1.9 introduced <code>BasicObject</code> as <code>Object</code>&#8216;s superclass.)</p>

<p>The <code>Class</code> class has the following inheritance hierarchy:</p>

<pre><code>Class &lt; Module &lt; Object &lt; BasicObject
</code></pre>

<p>Anything that&#8217;s a class (so <code>BasicObject</code>, <code>Object</code>, <code>Module</code>, <code>Class</code>, <code>Animal</code> and <code>Dog</code> here) has <code>Class</code> as its class &#8211; they&#8217;re all instances of <code>Class</code>. Any other objects have their own classes. So <code>fido</code> above will have a class of <code>Dog</code>, <code>3</code> has a class of <code>Fixnum</code>, &#8220;an awesome string&#8221; has a class of <code>String</code>, etc. All those classes inherit from <code>Object</code> ultimately (although classes like <code>Fixnum</code> have their own complex inheritance hierarchies).</p>

<p>To finish things off, here&#8217;s a little diagram of what I&#8217;ve just described, showing all the class and superclass links.</p>

<p><a href="http://skilldrick.co.uk/wp-content/uploads/2011/08/Ruby.png"><img src="http://skilldrick.co.uk/wp-content/uploads/2011/08/Ruby.png" alt="The Ruby object model" title="The Ruby object model" width="465" height="306" class="aligncenter size-full wp-image-1295" /></a></p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/00LrVLusEug" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/08/understanding-the-ruby-object-model/</feedburner:origLink></item>
		<item>
		<title>Reflections from a Kindle owner</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/U-WC4XtWrOI/</link>
		<comments>http://skilldrick.co.uk/2011/08/kindle/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 14:59:12 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1304</guid>
		<description><![CDATA[Back in April I got a Kindle for my birthday. It&#8217;s changed my tech-life massively, and I thought I&#8217;d write a few words on that here, from a developer&#8217;s perspective. (Disclaimer: the Amazon links in this blog post are Amazon Affiliate links, which means I get a percentage of any sales resulting from you following [...]]]></description>
			<content:encoded><![CDATA[<p>Back in April I got a <a href="http://www.amazon.co.uk/gp/product/B002LVUWFE/ref=as_li_ss_tl?ie=UTF8&amp;tag=skilldrick-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=B002LVUWFE">Kindle</a> for my birthday. It&#8217;s changed my tech-life massively, and I thought I&#8217;d write a few words on that here, from a developer&#8217;s perspective.</p>

<p>(Disclaimer: the Amazon links in this blog post are Amazon Affiliate links, which means I get a percentage of any sales resulting from you following them. If you don&#8217;t feel I deserve that percentage, then feel free to go to <a href="http://www.amazon.co.uk">amazon.co.uk</a> or <a href="http://www.amazon.com">amazon.com</a> [non-affiliate links] and I&#8217;m sure Amazon will be happy to help you find a Kindle.)</p>

<h3>Books</h3>

<p>I now read most books on my Kindle. In many ways it&#8217;s a better experience than a paper book &#8211; it&#8217;s much lighter than the average programming book, I can rest it on my leg without having to hold it open, turning pages is easier, and with a <a href="http://www.amazon.co.uk/gp/product/B004JXVOFE/ref=as_li_ss_tl?ie=UTF8&amp;tag=skilldrick-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=B004JXVOFE">cover with built-in light</a> I can read in the dark (e.g. I can read in bed without waking up my girlfriend).</p>

<p>It does have its downsides in comparison to a paper book as well &#8211; navigating between different spots isn&#8217;t as seamless/natural, even with hyperlinks; code listings often wrap (I usually view these in landscape mode, but even then it&#8217;s often not enough); it&#8217;s significantly more valuable than a book, so I need to take a lot more care with it; some nuances of book formatting don&#8217;t always make it through the conversion; and although it&#8217;s got a massive battery life, that&#8217;s still a consideration.</p>

<p>When I&#8217;m buying a new book online now though, I don&#8217;t hesitate to get the Kindle version (if it&#8217;s available). The benefits far outweigh the downsides.</p>

<h3>Magazines</h3>

<p>I&#8217;ve been wanting to read the <a href="http://pragprog.com/magazines">Prag Pub magazines</a> for a long time now, but I don&#8217;t like to sit in my computer and leaf through PDFs (and I certainly didn&#8217;t want to print them all out onto paper). Luckily, they&#8217;re all available for free download in Kindle format, so over the past few months I&#8217;ve gradually been catching up with the back issues. It&#8217;s a great magazine and I&#8217;d highly recommend it, whatever your e-reader of choice.</p>

<h3>Blogs and essays</h3>

<p><a href="http://www.instapaper.com/">Instapaper</a> is an essential tool for any Kindle owner. It gives you a &#8220;Read Later&#8221; bookmarklet, which enables you to mark any article to be read later. You can then tell Instapaper to deliver a collection of your unread articles to your Kindle regularly, for free! This has made such a difference to me. I come across so much good material online that I want to read, through Google Reader and Twitter, but when I&#8217;m at the computer I don&#8217;t just want to sit there reading blog after blog. Having those interesting articles delivered to my Kindle automatically means I don&#8217;t need to think at all &#8211; I can just read them when I feel like it.</p>

<p>In the same vein, I use Instapaper to catch up on older essays, such as <a href="http://www.paulgraham.com/articles.html">Paul Graham&#8217;s massive collection</a> or <a href="http://steve-yegge.blogspot.com/">Steve Yegge&#8217;s drunken blog rants</a>, that I&#8217;ve been wanting to read for a long time.</p>

<h3>Free 3G</h3>

<p>What a tongue-twister. It&#8217;s amazing though &#8211; if you get <a href="http://www.amazon.co.uk/gp/product/B002LVUWFE/ref=as_li_ss_tl?ie=UTF8&amp;tag=skilldrick-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=B002LVUWFE">the 3G version</a>, Amazon give you free web access, wherever you are. <strong>FREE</strong>. It still astounds me. It&#8217;s not particularly fast, and looks pretty ugly, but it works. For looking stuff up on Wikipedia it&#8217;s great.</p>

<h3>Conclusion</h3>

<p>If you&#8217;ve got an e-reader, or something e-reader-like, e.g. an iPhone/iPad/Android something, I&#8217;m not going to tell you you need a Kindle as well. Maybe you do, maybe you don&#8217;t. But for those that don&#8217;t have any way of reading electronic material away from a computer, I&#8217;d highly recommend getting one. It might change your life :)</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/U-WC4XtWrOI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/08/kindle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/08/kindle/</feedburner:origLink></item>
		<item>
		<title>Easy functional programming in JavaScript with Underscore.js — part 2</title>
		<link>http://feedproxy.google.com/~r/skilldrick/~3/ZkQLgB5zOhk/</link>
		<comments>http://skilldrick.co.uk/2011/07/easy-functional-programming-in-javascript-with-underscore-js-part-2/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 08:37:34 +0000</pubDate>
		<dc:creator>Skilldrick</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://skilldrick.co.uk/?p=1259</guid>
		<description><![CDATA[In the previous post I showed how good use of Underscore.js&#8216;s map function can substantially increase the quality of your code. This time I&#8217;ll explore some other functional collection favourites: reduce, select and all. Reduce that array over a low heat until thick and creamy Have you ever had to do something like this? Basically, [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://skilldrick.co.uk/2011/07/easy-functional-programming-in-javascript-with-underscore-js-part-1/">previous post</a> I showed how good use of <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>&#8216;s <code>map</code> function can substantially increase the quality of your code. This time I&#8217;ll explore some other functional collection favourites: <code>reduce</code>, <code>select</code> and <code>all</code>.</p>

<h3>Reduce that array over a low heat until thick and creamy</h3>

<p>Have you ever had to do something like this?</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/zMCgP/embedded/"></iframe>

<p>Basically, this is going through some kind of list of values, calculating a new value from each one and accumulating some kind of answer. It&#8217;s a common pattern, and there&#8217;s an Underscore function for it! This is exactly what <code>_.reduce</code> was built for:</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/qsPJ6/embedded/"></iframe>

<p><code>_.reduce</code> takes a collection of values, a function, and a starting value. In this case, the starting value is 0 because we&#8217;re building up a running total of numbers. The function is passed the running total (which starts at 0) and each element of the collection. The return value of the function becomes the next value for the running total. <code>_.reduce</code> doesn&#8217;t just need to be used on numbers:</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/HeuZm/embedded/"></iframe>

<p>In this case, I&#8217;m reducing an array of animal objects into a single string. Of course there are plenty of ways to do the above, but this shows you the flexibility of <code>_.reduce</code>.</p>

<h3>Bo Selecta!</h3>

<p>Imagine you have an array of strings, but you want a new array of strings that start with a capital letter. You might do that like this:</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/m59CS/embedded/"></iframe>

<p>Now, you won&#8217;t be surprised to learn that there&#8217;s an Underscore function for doing just that: introducing <code>_.select</code>!</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/Vs5GX/embedded/"></iframe>

<p>Isn&#8217;t that much cleaner? Again, we&#8217;re abstracting away the minutiae of selecting elements from a list, and just declaring that we want the elements that start with a capital letter. Simples!</p>

<h3>All the single ladies</h3>

<p>Sometimes you just want to know a yes or no answer about a collection. For example, are all my ladies single?</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/LEzsw/1/embedded/"></iframe>

<p>Hell yes they are. You can read <code>_.all</code> as &#8220;is this statement true about all the elements in my collection?&#8221; In the same way, <code>_.any</code> can be read as &#8220;is this statement true about any of the elements in my collection?&#8221;</p>

<h3>What about this?</h3>

<p>Here&#8217;s a useful little tip. When you&#8217;re working with nested functions in an object context, you&#8217;ll find that <code>this</code> in the nested function doesn&#8217;t refer to the object&#8217;s context, but the global context. The standard way to deal with this is a line like <code>that = this;</code> or <code>self = this;</code>. There&#8217;s a nicer way to do this with Underscore. Nearly all the functions for working on collections take an optional <code>context</code> argument, which specifies what <code>this</code> will be inside the function. Here&#8217;s an example:</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/WCsbb/embedded/"></iframe>

<p>Because we&#8217;re passing the value of <code>this</code> to <code>_.select</code>, the function passed to <code>_.select</code> is working with the correct <code>this</code>. Otherwise, <code>this.maxAge</code> would be undefined.</p>

<h3>Learning more</h3>

<p>I&#8217;ve only gone into some of the functionality that Underscore offers. <a href="http://documentcloud.github.com/underscore/">The docs</a> are very approachable and clear &#8211; I urge you to <a href="http://documentcloud.github.com/underscore/">go and read them</a>.</p>

<p><a href="https://twitter.com/SamirTalwar">@SamirTalwar</a> on Twitter recommended that I show the alternative syntax for Underscore, <a href="http://documentcloud.github.com/underscore/#styles">as described in the docs</a>. Basically, instead of doing:</p>

<pre><code>_.map(collection, func);
</code></pre>

<p>you can do:</p>

<pre><code>_(collection).map(func);
</code></pre>

<p>And, coupled with <code>.chain()</code> you can do awesome chaining things like this:</p>

<iframe style="width: 95%; height: 200px" src="http://jsfiddle.net/skilldrick/pwadd/embedded/"></iframe>

<p>Pretty cool. Anyway, use whichever feels right to you. Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/skilldrick/~4/ZkQLgB5zOhk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://skilldrick.co.uk/2011/07/easy-functional-programming-in-javascript-with-underscore-js-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://skilldrick.co.uk/2011/07/easy-functional-programming-in-javascript-with-underscore-js-part-2/</feedburner:origLink></item>
	</channel>
</rss>

