<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://jonathanleighton.com/</id>
  <title>Jon Leighton</title>
  <updated>2012-02-19T00:00:00Z</updated>
  <link rel="alternate" href="http://jonathanleighton.com/"/>
  <link rel="self" href="http://jonathanleighton.com/articles.xml"/>
  <author>
    <name>Jon Leighton</name>
    <uri>http://jonathanleighton.com/</uri>
  </author>
  <entry>
    <id>tag:jonathanleighton.com,2012-02-19:/articles/2012/finding-methods/</id>
    <title type="html">Tracking down method definitions in Ruby</title>
    <published>2012-02-19T00:00:00Z</published>
    <updated>2012-02-19T00:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/finding-methods/"/>
    <content type="html">&lt;p&gt;One of my favourite features of Ruby 1.9 is the &lt;code&gt;#source_location&lt;/code&gt;
method of &lt;code&gt;Proc&lt;/code&gt; and &lt;code&gt;Method&lt;/code&gt;. Let me explain. Often I am confronted
with a large code base (usually Rails), and want to figure out
exactly where some method on some object is defined. Rails has many dark
corners, and sometimes finding things isn’t entirely straightforward.&lt;/p&gt;

&lt;p&gt;A trick that I often use is to get a &lt;code&gt;Method&lt;/code&gt; object for the method that
I want to find, and then to print out its location. Like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Foo&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;bar&lt;/span&gt;
    &lt;span class="s"&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="k"&gt;omg, where am I?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

p &lt;span class="co"&gt;Foo&lt;/span&gt;.new.method(&lt;span class="sy"&gt;:bar&lt;/span&gt;).source_location &lt;span class="c"&gt;# found you!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Recently I was trying to find out where the &lt;code&gt;#flash&lt;/code&gt; method of
&lt;code&gt;ActionDispatch::TestRequest&lt;/code&gt; was defined. It’s not defined in the main
files that contain the definitions for &lt;code&gt;ActionDispatch::TestRequest&lt;/code&gt; or
&lt;code&gt;ActionDispatch::Request&lt;/code&gt; (which is the superclass).&lt;/p&gt;

&lt;p&gt;So I pulled out my standard tool:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;r = &lt;span class="co"&gt;ActionDispatch&lt;/span&gt;::&lt;span class="co"&gt;TestRequest&lt;/span&gt;.new
p r.method(&lt;span class="sy"&gt;:flash&lt;/span&gt;).source_location&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It didn’t work:&lt;/p&gt;

&lt;pre&gt;ArgumentError: wrong number of arguments (1 for 0)&lt;/pre&gt;

&lt;p&gt;It turns out that &lt;code&gt;ActionDispatch::Request&lt;/code&gt; has its own &lt;code&gt;method&lt;/code&gt; method,
which relates to the HTTP method. Hence Ruby’s &lt;code&gt;method&lt;/code&gt; method is
overridden.&lt;/p&gt;

&lt;p&gt;However, all was not lost! Ruby also makes it possible to get a
reference to a method definition that isn’t bound to any particular
object. This is called an &lt;code&gt;UnboundMethod&lt;/code&gt;, and you can’t call it until
you bind it to some object. So I was able to get an unbound reference to
the original &lt;code&gt;method&lt;/code&gt; method and then bind it to my object. Like so:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;r = &lt;span class="co"&gt;ActionDispatch&lt;/span&gt;::&lt;span class="co"&gt;TestRequest&lt;/span&gt;.new
meth = &lt;span class="co"&gt;Object&lt;/span&gt;.instance_method(&lt;span class="sy"&gt;:method&lt;/span&gt;)
p meth.bind(r).call(&lt;span class="sy"&gt;:flash&lt;/span&gt;).source_location &lt;span class="c"&gt;# found it!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It turns out that the &lt;code&gt;Request&lt;/code&gt; class is reopened in
&lt;code&gt;actionpack/lib/action_dispatch/middleware/flash.rb&lt;/code&gt; and the &lt;code&gt;flash&lt;/code&gt;
method gets defined there.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-02-19:/articles/2012/stop-test-unit-autorun/</id>
    <title type="html">How to prevent Ruby's test/unit library from autorunning your tests</title>
    <published>2012-02-19T00:00:00Z</published>
    <updated>2012-02-19T00:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/stop-test-unit-autorun/"/>
    <content type="html">&lt;p&gt;Today I had a situation where I wanted to use
&lt;a href="https://github.com/tmm1/perftools.rb"&gt;perftools.rb&lt;/a&gt; to profile a test
suite, which was written with Ruby’s &lt;code&gt;test/unit&lt;/code&gt; library.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;test/unit&lt;/code&gt; installs an &lt;code&gt;at_exit&lt;/code&gt; hook which runs all the tests before
Ruby exits. This was problematic, because it meant that all the tests
ran &lt;em&gt;after&lt;/em&gt; perftools had already finished its profile.&lt;/p&gt;

&lt;p&gt;So I wanted to turn off the autorunning and explicitly run the test
suite. I found out how to monkey-patch &lt;code&gt;test/unit&lt;/code&gt; to achieve this, and
figured it might be useful to someone in the future, so:&lt;/p&gt;

&lt;h2 id="ruby-19"&gt;Ruby 1.9&lt;/h2&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;require &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;test/unit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Test::Unit::Runner&lt;/span&gt;
  &lt;span class="cv"&gt;@@stop_auto_run&lt;/span&gt; = &lt;span class="pc"&gt;true&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;FooTest&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;TestCase&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;test_foo&lt;/span&gt;
    assert &lt;span class="pc"&gt;true&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;Runner&lt;/span&gt;.new.run(&lt;span class="pc"&gt;ARGV&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="ruby-18"&gt;Ruby 1.8&lt;/h2&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;require &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;test/unit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="r"&gt;module&lt;/span&gt; &lt;span class="cl"&gt;Test::Unit&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="pc"&gt;self&lt;/span&gt;.&lt;span class="fu"&gt;run?&lt;/span&gt;; &lt;span class="pc"&gt;true&lt;/span&gt;; &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;FooTest&lt;/span&gt; &amp;lt; &lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;TestCase&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;test_foo&lt;/span&gt;
    assert &lt;span class="pc"&gt;true&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;

&lt;span class="co"&gt;Test&lt;/span&gt;::&lt;span class="co"&gt;Unit&lt;/span&gt;::&lt;span class="co"&gt;AutoRunner&lt;/span&gt;.run&lt;/code&gt;&lt;/pre&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-01-09:/articles/2012/encapsulating-hashes/</id>
    <title type="html">Hashes and encapsulation</title>
    <published>2012-01-09T00:00:00Z</published>
    <updated>2012-01-09T00:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/encapsulating-hashes/"/>
    <content type="html">&lt;p&gt;Earlier today I made an off-the-cuff remark about encapsulation on
twitter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/jonleighton/status/156424794152251393" class="image-link"&gt;
&lt;img src="twitter.png" alt="FYI: if you're accessing hash elements via obj.hashthings['foo'] rather than obj.hashthing('foo'), you're breaking encapsulation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then &lt;a href="https://twitter.com/joshkalderimis"&gt;Josh&lt;/a&gt;, not realising that I
was simply casting judgement down from my ivory tower, asked for more
than 140 characters worth of explanation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/joshkalderimis/status/156425566860476416" class="image-link"&gt;
&lt;img src="response.png" alt="@jonleighton FYI its nice if you explain a bit more :) blog post?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I agreed. Here is that explanation.&lt;/p&gt;

&lt;p&gt;Suppose you have an object representing a
&lt;a href="http://en.wikipedia.org/wiki/Document_Object_Model"&gt;DOM&lt;/a&gt; element. DOM
elements have attributes, which you store as a hash. Something like
this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;DOMElement&lt;/span&gt;
  attr_reader &lt;span class="sy"&gt;:attributes&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
      [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
    }]
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In your code that uses &lt;code&gt;DOMElement&lt;/code&gt;, you access an attribute like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;element.attributes[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;margin-left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you try to access an attribute that does not exist, &lt;code&gt;nil&lt;/code&gt; will be
returned.&lt;/p&gt;

&lt;p&gt;Later on, you realise that you are checking for &lt;code&gt;nil&lt;/code&gt; in lots of places
where you use attributes. Listening to the code smell, you decide that
missing attributes should instead return a &lt;a href="http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/"&gt;null
object&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What do you do? Well, your only option is to add a &lt;a href="http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-default-3D"&gt;default
proc&lt;/a&gt;
to the &lt;code&gt;Hash&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
  [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
}]
&lt;span class="iv"&gt;@attributes&lt;/span&gt;.default_proc = proc { &lt;span class="co"&gt;NullDOMAttributes&lt;/span&gt;.new }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You have now made your object impossible to marshal without using
insane hackery, since procs cannot be marshalled.&lt;/p&gt;

&lt;p&gt;The more important point, though, is that by allowing users to dip into
the &lt;code&gt;@attributes&lt;/code&gt; hash like this, you are failing to encapsulate the
internal implementation of your &lt;code&gt;DOMElement&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;This presents another problem: an unrelated dodgy bit of code could
remove an attribute from the element, without your &lt;code&gt;DOMElement&lt;/code&gt; object
realising!&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;el.attributes.delete(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;margin-left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you pass around the same hash between lots of different objects, that
hash is quite likely to get mutated at some point in my experience, and
when that happens it will affect all the other objects relying on it.&lt;/p&gt;

&lt;p&gt;Yes, you could call &lt;code&gt;@attributes.dup&lt;/code&gt; every time the &lt;code&gt;attributes&lt;/code&gt; method
is called, but that’s hardly very efficient when we only want to get at
a single attribute.&lt;/p&gt;

&lt;p&gt;If the code had been written in an encapsulated manner to begin with:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;DOMElement&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt; = &lt;span class="co"&gt;Hash&lt;/span&gt;[parse_attributes.map { |name, value|
      [name, &lt;span class="co"&gt;DOMAttribute&lt;/span&gt;.new(name, value)]
    }]
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attributes&lt;/span&gt;
    &lt;span class="iv"&gt;@attributes&lt;/span&gt;.dup
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attribute&lt;/span&gt;(name)
    &lt;span class="iv"&gt;@attributes&lt;/span&gt;[name]
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It would be straightforward to add the null object without further consequences:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;attribute&lt;/span&gt;(name)
  &lt;span class="iv"&gt;@attributes&lt;/span&gt;.fetch(name) { &lt;span class="co"&gt;NullDOMElement&lt;/span&gt;.new }
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, exposing internal hashes can be convenient, but you should always
think twice before doing so and realise that you might regret the
decision at a later date.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2012-01-03:/articles/2012/announcing-poltergeist/</id>
    <title type="html">Poltergeist: A PhantomJS driver for Capybara</title>
    <published>2012-01-03T00:00:00Z</published>
    <updated>2012-01-03T00:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2012/announcing-poltergeist/"/>
    <content type="html">&lt;p&gt;This announcement is coming way later than I had originally intended.
Last October I started experimenting with the idea of writing a
driver for &lt;a href="https://github.com/jnicklas/capybara"&gt;Capybara&lt;/a&gt; that would use &lt;a href="http://www.phantomjs.org/"&gt;PhantomJS&lt;/a&gt; as the
browser.&lt;/p&gt;

&lt;p&gt;Initially the biggest problem was addressing the issue of how to
communicate between a Ruby process and a PhantomJS process. But then it
hit me: PhantomJS gives you a browser environment, so you can do
everything you can do in a browser, and you can do Web Sockets in a
browser. So I used Web Sockets.&lt;/p&gt;

&lt;p&gt;After hacking away for a while I eventually had a pretty complete driver.
But I was being plagued by segfaults that were coming from WebKit’s
JavaScriptCore JS engine. Thus began months of poking C++ code and
getting far too comfortable with &lt;code&gt;gdb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I tried the Qt 4.8 RC (it has since been properly released) and found
that the JSC segfault had gone, but now there was a new segfault.
After much hair-pulling I found &lt;a href="https://github.com/ariya/phantomjs/commit/38269c7b3566179dd9a825230a4ddfad95cf1cdc"&gt;a
workaround&lt;/a&gt;.
But I was still left with another problem: it wasn’t possible to attach
files to &lt;code&gt;&amp;lt;input&amp;gt;&lt;/code&gt; elements against Qt 4.8. After yet more hair-pulling I &lt;a href="https://github.com/ariya/phantomjs/commit/00a8e72fb306d0cedca399425a11b862f2fb27ea"&gt;found the
culprit&lt;/a&gt;
for that one, too.&lt;/p&gt;

&lt;p&gt;Which leads me to the point where I am now finally happy to invite you,
dear reader, to try out &lt;a href="https://github.com/jonleighton/poltergeist"&gt;my humble little Capybara
driver&lt;/a&gt;. Let me know how you
get on!&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jonathanleighton.com,2011-08-30:/articles/2011/initialize_clone-initialize_dup-and-initialize_copy-in-ruby/</id>
    <title type="html">&lt;code&gt;initialize_clone&lt;/code&gt;, &lt;code&gt;initialize_dup&lt;/code&gt; and &lt;code&gt;initialize_copy&lt;/code&gt; in Ruby</title>
    <published>2011-08-29T23:00:00Z</published>
    <updated>2011-08-29T23:00:00Z</updated>
    <link rel="alternate" href="http://jonathanleighton.com/articles/2011/initialize_clone-initialize_dup-and-initialize_copy-in-ruby/"/>
    <content type="html">&lt;p&gt;Ruby has two methods for creating shallow copies of objects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Object#clone&lt;/code&gt;&lt;/strong&gt; copies the object, including its &lt;em&gt;frozen&lt;/em&gt; state&lt;/li&gt;
  &lt;li&gt;
&lt;strong&gt;&lt;code&gt;Object#dup&lt;/code&gt;&lt;/strong&gt; copies the object, not including its &lt;em&gt;frozen&lt;/em&gt; state&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;In general &lt;code&gt;clone&lt;/code&gt; is meant to be a more “exact” mechanism for copying objects, whereas &lt;code&gt;dup&lt;/code&gt;
is often implemented by simply creating a new instance of the relevant class with the appropriate
parameters. Both &lt;code&gt;clone&lt;/code&gt; and &lt;code&gt;dup&lt;/code&gt; copy the &lt;em&gt;tainted&lt;/em&gt; state of the object. &lt;code&gt;clone&lt;/code&gt; copies
the singleton class (if any), whereas &lt;code&gt;dup&lt;/code&gt; does not.&lt;/p&gt;

&lt;h2 id="initialising-copies"&gt;Initialising copies&lt;/h2&gt;

&lt;p&gt;Sometimes it is useful to be able to specify some initialisation code that should be run when
an object is copied. For example, suppose an object tracks its own internal state in some way,
you may wish to reset this state when the object is copied.&lt;/p&gt;

&lt;p&gt;Ruby 1.9 has 3 methods to help you do this: &lt;code&gt;initialize_clone&lt;/code&gt;,
&lt;code&gt;initialize_dup&lt;/code&gt; and &lt;code&gt;initialize_copy&lt;/code&gt;. At present, there is no documentation (that I can find),
so I had to do a bit of digging through C code to work out the exact behaviour.&lt;/p&gt;

&lt;p&gt;The implementation is expressed by the following psuedo-code:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-ruby"&gt;&lt;span class="r"&gt;class&lt;/span&gt; &lt;span class="cl"&gt;Object&lt;/span&gt;
  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;clone&lt;/span&gt;
    clone = &lt;span class="pc"&gt;self&lt;/span&gt;.class.allocate

    clone.copy_instance_variables(&lt;span class="pc"&gt;self&lt;/span&gt;)
    clone.copy_singleton_class(&lt;span class="pc"&gt;self&lt;/span&gt;)

    clone.initialize_clone(&lt;span class="pc"&gt;self&lt;/span&gt;)
    clone.freeze &lt;span class="r"&gt;if&lt;/span&gt; frozen?

    clone
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;dup&lt;/span&gt;
    dup = &lt;span class="pc"&gt;self&lt;/span&gt;.class.allocate
    dup.copy_instance_variables(&lt;span class="pc"&gt;self&lt;/span&gt;)
    dup.initialize_dup(&lt;span class="pc"&gt;self&lt;/span&gt;)
    dup
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize_clone&lt;/span&gt;(other)
    initialize_copy(other)
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize_dup&lt;/span&gt;(other)
    initialize_copy(other)
  &lt;span class="r"&gt;end&lt;/span&gt;

  &lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;initialize_copy&lt;/span&gt;(other)
    &lt;span class="c"&gt;# some internal stuff (don't worry)&lt;/span&gt;
  &lt;span class="r"&gt;end&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;initialize_copy&lt;/code&gt; runs for both &lt;code&gt;clone&lt;/code&gt; and &lt;code&gt;dup&lt;/code&gt;, but &lt;em&gt;it is called by &lt;code&gt;initialize_clone&lt;/code&gt;
and &lt;code&gt;initialize_dup&lt;/code&gt;&lt;/em&gt;. Therefore, if you implement your own version of &lt;code&gt;initialize_clone&lt;/code&gt; or
&lt;code&gt;initialize_dup&lt;/code&gt;, it is advisable to call &lt;code&gt;super&lt;/code&gt; to make sure that &lt;code&gt;initialize_copy&lt;/code&gt; is also
called.&lt;/p&gt;

&lt;h2 id="ruby-18"&gt;Ruby 1.8&lt;/h2&gt;

&lt;p&gt;Ruby 1.8 behaves in roughly the same way, but &lt;em&gt;it does not have &lt;code&gt;initialize_dup&lt;/code&gt; or
&lt;code&gt;initialize_clone&lt;/code&gt;&lt;/em&gt; built-in.&lt;/p&gt;

&lt;p&gt;It would be possible to implement some sort of backport in pure Ruby, but harder to get the
semantics to be identical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;Object#clone&lt;/code&gt;, the clone is frozen &lt;em&gt;after&lt;/em&gt; &lt;code&gt;initialize_clone&lt;/code&gt; is called&lt;/li&gt;
  &lt;li&gt;The backport would probably be implemented by calling &lt;code&gt;super&lt;/code&gt; and then calling &lt;code&gt;initialize_clone&lt;/code&gt;
or &lt;code&gt;initialize_dup&lt;/code&gt; (which would be defined in pure Ruby on &lt;code&gt;Object&lt;/code&gt;). But note that the &lt;code&gt;super&lt;/code&gt;
call will result in &lt;code&gt;initialize_copy&lt;/code&gt; being called
already, which is inherently different from the 1.9 implementation where &lt;code&gt;initialize_clone&lt;/code&gt; and
&lt;code&gt;initialize_dup&lt;/code&gt; are in charge of calling &lt;code&gt;initialize_copy&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;h2 id="ouch-head-hurts"&gt;Ouch, head hurts&lt;/h2&gt;

&lt;p&gt;Yeah, never mind really. I was just curious and thought I’d share my findings.&lt;/p&gt;</content>
  </entry>
</feed>


