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

<channel>
	<title>transfixed but not dead! &#187; smalltalk</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/smalltalk/feed/" rel="self" type="application/rss+xml" />
	<link>http://transfixedbutnotdead.com</link>
	<description>my ramblings on life, work &#38; anything left in-between</description>
	<lastBuildDate>Fri, 03 Feb 2012 16:03:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='transfixedbutnotdead.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0a317653027efb1ab2bf8adde3dcb067?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>transfixed but not dead! &#187; smalltalk</title>
		<link>http://transfixedbutnotdead.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://transfixedbutnotdead.com/osd.xml" title="transfixed but not dead!" />
	<atom:link rel='hub' href='http://transfixedbutnotdead.com/?pushpress=hub'/>
		<item>
		<title>Using Moose Roles to create Singleton Methods</title>
		<link>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/</link>
		<comments>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 14:14:09 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[singleton methods]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=388</guid>
		<description><![CDATA[Randal Schwartz recently posted this bit of Smalltalk code on his Methods and Messages blog p := 4 @ 3. p changeClassTo: (p class copy superclass: p class). p class methodDictionary at: #negated put: (p class methodDictionary at: #transpose). p negated My exposure to Smalltalk is limited to a quick look at Seaside web framework [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=388&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Randal Schwartz recently posted this bit of <a href="http://methodsandmessages.vox.com/library/post/why-i-like-smalltalk-lots-of-reflection.html">Smalltalk code</a> on his <a href="http://methodsandmessages.vox.com/">Methods and Messages blog</a></p>
<p><code>p := 4 @ 3.<br />
p changeClassTo: (p class copy superclass: p class).<br />
p class methodDictionary at: #negated put: (p class methodDictionary at: #transpose).<br />
p negated<br />
</code></p>
<p>My exposure to Smalltalk is limited to a quick look at <a href="http://seaside.st">Seaside web framework</a> which is a bit like learning Rails instead of starting with Ruby <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   </p>
<p>I can just about grok what this code does.  Thankfully <a href="http://talklikeaduck.denhaven2.com">Rick DeNatale</a> provided a nice <a href="http://talklikeaduck.denhaven2.com/2009/05/30/singleton-methods-in-smalltalk-and-ruby">Ruby conversion</a></p>
<p>Here is Rick&#8217;s subset of Smalltalk&#8217;s Point which replicates what the first Smalltalk line &#8220;p := 4 @ 3.&#8221; does:</p>
<p><pre class="brush: ruby;">
class Point
  attr_accessor :X, :y    # :X should be lowercase but Wordpress keeps converting it to a smiley!

  def initialize(x, y)
    @x, @y = x, y
  end

  def negated
    self.class.new(-x, -y)
  end

  def transpose
    self.class.new(y, x)
  end

  def inspect
    &quot;#{@x} @ #{@y}&quot;
  end
end


p = Point.new(4,3)

p.negated          # =&gt; -4 @ -3
p.transpose        # =&gt; 3 @ 4
</pre></p>
<p>So <em>negated</em> &amp; <em>transpose</em> methods return new Point objects so all very straightforward so far.</p>
<p>In the Smalltalk code the object <em>negated</em> method gets amended to use the <em>transpose</em> method instead.<br />
You can do this in Ruby by opening up the objects method like so:</p>
<p><pre class="brush: ruby;">
def p.negated
  transpose
end

p.negated          # =&gt; 3 @ 4
p.transpose        # =&gt; 3 @ 4
</pre></p>
<p>Nice eh!  The Ruby world refer to this has a Singleton Method.  These are methods which are defined in the object and not the class.   </p>
<p><em>I highly recommend watching this <a href="http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4">excellent video talk</a> by Dave Thomas which provides excellent lucidity to how Ruby objects work.</em></p>
<p>Now how about Perl?   I suspect you can use some MOP meta magic to do same but did you know that <a href="http://search.cpan.org/dist/Moose/lib/Moose/Role.pm">Moose Roles</a> can be applied directly to objects?</p>
<p>Here&#8217;s is the Ruby Point code in Perl using classic Moose.<br />
<pre class="brush: perl;">
{
    package Point;
    use Moose;

    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );

    sub negated {
        my $self = shift;
        $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y );
    }

    sub transpose {
        my $self = shift;
        $self-&gt;new( x =&gt; $self-&gt;y, y =&gt; $self-&gt;x );
    }

    sub inspect { say &quot;$_[0]-&gt;{x} \@ $_[0]-&gt;{y}&quot; }
    
    no Moose;
}


my $p = Point-&gt;new( x =&gt; 4, y =&gt; 3 );

$p-&gt;negated-&gt;inspect;     # =&gt; -4 @ -3
$p-&gt;transpose-&gt;inspect;   # =&gt; 3 @ 4
</pre></p>
<p>So all works same as the Ruby example.   Now if we create a role like so:</p>
<p><pre class="brush: perl;">
{
    package Negated;
    use Moose::Role;
    
    requires 'transpose';
    
    sub negated { 
        my $self = shift;
        $self-&gt;transpose;
    }
    
    no Moose::Role;
}
</pre></p>
<p>You can then &#8220;apply&#8221; it to any object like so:<br />
<pre class="brush: perl;">
Negated-&gt;meta-&gt;apply( $p );
$p-&gt;negated-&gt;inspect;     # =&gt; 3 @ 4
$p-&gt;transpose-&gt;inspect;   # =&gt; 3 @ 4
</pre></p>
<p>Lovely jubbly!   </p>
<p>And as we&#8217;ve gone down this Moose Role route we might as well wrap it all up like this:<br />
<pre class="brush: perl;">
{
    package Point;
    use Moose;

    with qw/DoesNegated DoesTranspose/;
    
    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );

    sub inspect { say &quot;$_[0]-&gt;{x} \@ $_[0]-&gt;{y}&quot; }
    
    no Moose;
}

{
    package DoesNegated;
    use Moose::Role;
        
    sub negated {
        my $self = shift;
        $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y );
    }
    
    no Moose::Role;
}

{
    package DoesTranspose;
    use Moose::Role;
        
    sub transpose {
        my $self = shift;
        $self-&gt;new( x =&gt; $self-&gt;y, y =&gt; $self-&gt;x );
    }
    
    no Moose::Role;
}

my $p = Point-&gt;new( x =&gt; 4, y =&gt; 3 );

DoesTranspose-&gt;meta-&gt;apply( $p, alias =&gt; { transpose =&gt; 'negated' } );

$p-&gt;negated-&gt;inspect;     # =&gt; 3 @ 4
$p-&gt;transpose-&gt;inspect;   # =&gt; 3 @ 4
</pre></p>
<p>This time I used the &#8220;alias&#8221; option to rename the applied method on the Moose hoof <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>And u can keep applying roles to  objects at your hearts content adding or replacing methods<br />
as many times as u like.</p>
<p>/I3az/</p>
<p>Some references:</p>
<ul>
<li><a href="http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Roles/Recipe3.pod">Moose Cookbook Roles Recipe 3</a>
<li><a href="http://stackoverflow.com/questions/813266/what-to-do-with-an-object-that-should-no-longer-be-used-in-perl/817666#817666">my Stackoverflow answer about applying roles on objects</a>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/388/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=388&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
<enclosure url="http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4" length="285514197" type="video/mp4" />
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
	</channel>
</rss>
