<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Trypticon: Tag ruby</title>
    <link>http://trypticon.org/articles/tag/ruby?tag=ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>If it ain't broke, break it.</description>
    <item>
      <title>Just Another Monday Morning</title>
      <description>&lt;p&gt;Monday morning came with a request to change the company web site to American &amp;#8220;English&amp;#8221;.&lt;/p&gt;

&lt;p&gt;My first thought was that we&amp;#8217;re not a US company, so we shouldn&amp;#8217;t need to conform to US spelling.  The reasoning I got back was that US companies got scared off by British spelling.&lt;/p&gt;

&lt;p&gt;This reasoning sounds pretty ludicrous to me, and besides, if we switch to American spelling, won&amp;#8217;t that &amp;#8220;scare off&amp;#8221; the entire rest of the English speaking world?&lt;/p&gt;

&lt;p&gt;So I thought, OK&amp;#8230; maybe we can do this and yet not do this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/ruby
#
# Breaks proper English spelling for the benefit of yanks.
# Word list is incomplete, but covers enough to be useful for now.

ARGF.each_line do |line|
    line.sub!(/\b(amort|apolog|author|capital|critic|emphas|general|harmon|initial|maxim|minim|optim|real|recogn|visual)is(e|ed|es|er|or|ation)\b/, '\1iz\2')
    line.sub!(/\b(anal|catal|paral)ys(e|ed|es|er|or)\b/, '\1yz\2')
    line.sub!(/\b(arm|behavi|col|flav|harb|hon|hum|neighb|sav|savi)our(s?)\b/, '\1or\2')
    line.sub!(/\b(defen|licen|offen|preten)ce(s?)\b/, '\1se\2')
    line.sub!(/\b(calib|cent|fib|kilomet|lit|meag|met|theat)re(s?)\b/, '\1er\2')
    line.sub!(/\bpractis(e|ed|ing)\b/, 'practic\1')
    puts line;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;My only concern is that it might match an ID somewhere in the HTML which will mess the CSS up, but otherwise, it&amp;#8217;s looking like a damn good idea.&lt;/p&gt;</description>
      <pubDate>Mon, 14 May 2007 10:13:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:5e8de16b-be1b-459d-a5f0-3fb4c9ac6e85</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2007/05/14/just-another-monday-morning</link>
      <category>programming</category>
      <category>monday</category>
      <category>spelling</category>
      <category>english</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Action Messenger 0.2.0</title>
      <description>&lt;p&gt;Today&amp;#8217;s new version of &lt;a href="http://trypticon.org/software/actionmessenger/"&gt;Action Messenger&lt;/a&gt; makes things slightly easier to read when sending messages:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def prod(user)
  recipient user.jid
  subject   'Prod'
  body      'You told me to give you a prod when I updated...'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can send to multiple recipients too, just add them like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def spam
  recipients User.find_all.map { |u| u.jid }
  subject    'Buy chunky bacon'
  body       'Do I have a deal for you!'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These changes have been in trunk for some time, however someone recently made me aware that the documentation doesn&amp;#8217;t describe basic things like where the config file should be.  This has been properly documented now, and hence the new version being pushed out.&lt;/p&gt;</description>
      <pubDate>Fri, 23 Mar 2007 20:04:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:390f7155-8edb-49de-9c2c-60068a08bc97</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2007/03/23/action-messenger-0-2-0</link>
      <category>actionmessenger</category>
      <category>jabber</category>
      <category>software</category>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Directions for Action Messenger</title>
      <description>&lt;p&gt;Well, the first highly experimental release of &lt;a href="http://trypticon.org/software/actionmessenger/" title="Action Messenger"&gt;Action Messenger&lt;/a&gt; experienced a small amount of success, and a couple of people spotted some bugs which have been fixed in version 0.1.1.&lt;/p&gt;

&lt;p&gt;So now that everything works for small apps, how does it grow?  I do want to integrate into Typo,  and the default deployment for Typo uses FastCGI.  Whereas each FastCGI process is relatively long-lived, there are multiple instances, and they can&amp;#8217;t all be logged in at once [with the same resource.]&lt;/p&gt;&lt;p&gt;So I have a number of options for coping with having Action Messenger work from multiple Rails instances.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Have each instance login from a different resource.  This is the simplest approach, and the one which I&amp;#8217;m currently using on this site&amp;#8217;s Typo install: append the process ID onto the resource when logging in.  The main downside to this approach is that when I add presence support in, users who have added the bot would see all its multiple resources logged in at once.  Whereas this would start out fairly small (I use 5 instances on my Typo) for larger, more popular sites, you might end up with a few dozen bots online at the same time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have one instance of Rails nominate it as the master, and start a DRb server on a background thread.  The other instances would connect to the master when they need to send a message.  This would be the simplest approach for the end user, but seems difficult to implement.  Which server becomes the master?  Perhaps an instance simply tries to open a socket to the server, and if it fails, it tries to open a socket as the server.  In this fashion, there is always only one instance online, and when that instance goes offline (e.g. the process crashes) another one would pick it up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have the Rails app spawn a separate process to handle the server side of things, but otherwise do things exactly as #2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use something such as &lt;a href="http://opensvn.csie.org/ezra/rails/backgroundrb/" title="BackgrounDRb"&gt;BackgrounDRb&lt;/a&gt; to run the bot as one thread of a much more flexible server.  This approach is probably the most useful as things other than a Jabber bot might be run in this server, although at the moment I&amp;#8217;m not sure what.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write a simple, specialist Ruby bot, and require that bot to be started completely separately from Rails.  The Rails instances would then connect to the bot whenever they need something.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At the moment, #5 is my favourite option.  My main problem with this approach is &amp;#8211; how will the bot handle receiving messages?  Accessing the Rails models directly from the bot isn&amp;#8217;t the problem, the problem is coming up with an API just as clean as the current one.&lt;/p&gt;</description>
      <pubDate>Mon, 22 May 2006 22:16:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:86f30d27-e17a-403e-bb1a-08dee872330f</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2006/05/22/directions-for-action-messenger</link>
      <category>software</category>
      <category>jabber</category>
      <category>rails</category>
      <category>ruby</category>
      <category>actionmessenger</category>
    </item>
    <item>
      <title>Jabbering with Action Messenger</title>
      <description>&lt;p&gt;Want to send instant messages from your Ruby on Rails app with the minimum amount of code and the maximum amount of testability?&lt;/p&gt;

&lt;p&gt;Then say hello to&amp;#8230; &lt;a href="http://trypticon.org/software/actionmessenger/" title="Action Messenger"&gt;Action Messenger&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Some time ago, the &lt;a href="http://typosphere.org/" title="TypoSphere"&gt;Typo&lt;/a&gt; weblog software introduced a new feature, where notifications of new articles or comments could be delivered via XMPP, or as it&amp;#8217;s more commonly known, Jabber.  The one catch with the feature was that it depended on the older Jabber4R library, which under certain versions of Ruby, simply failed to work.  Jabber4R has serious bugs in there which cause it to deadlock under any newer version of Ruby.&lt;/p&gt;

&lt;p&gt;I wanted to use the feature rather badly at the time, so I &lt;a href="http://typosphere.org/trac/ticket/582" title="Typo ticket 582: Jabber session doesn't login"&gt;patched it&lt;/a&gt; to use &lt;a href="http://home.gna.org/xmpp4r/" title="XMPP4R: a newer XMPP client for Ruby"&gt;XMPP4R&lt;/a&gt; instead.  The patch still hasn&amp;#8217;t been accepted, allegedly due to a &lt;a href="http://article.gmane.org/gmane.comp.web.typo.user/2343"&gt;lack of tests&lt;/a&gt;.  Granted, unit testing is big in the Rails world, but the original Jabber notification code didn&amp;#8217;t have unit tests either, so I just see this as an enormous hypocrisy where the developers don&amp;#8217;t write their own tests, and think that it&amp;#8217;s everyone else&amp;#8217;s responsibility to write the tests for them.&lt;/p&gt;

&lt;p&gt;So in any case because of this, it&amp;#8217;s likely that under the current state of affairs, the existing Jabber support in Typo will remain broken indefinitely.&lt;/p&gt;

&lt;p&gt;After all of that, I started thinking about unit testing the Jabber messaging part.  The main problem is that a Jabber communication library generally requires a network connection to operate.  Whereas this is usually avoided by using mock objects, XMPP4R exposes rather a lot of different classes, and mocking the entire library out is probably just a little unreasonable.&lt;/p&gt;

&lt;p&gt;So what you do is write a layer which has less things to mock out, and that&amp;#8217;s the basis of Action Messenger.  However, I couldn&amp;#8217;t stop at merely having this layer for mocking.  I had to go and make it possible to send a Jabber message with three trivial lines of code (two in the messenger layer, setting up the recipient and the body, and one in the controller to tell the messenger to send the message.)&lt;/p&gt;

&lt;p&gt;So that&amp;#8217;s what Action Messenger is.  There isn&amp;#8217;t much to it right now, but what&amp;#8217;s there is heavily unit tested, and has convenience built-in for making your own code which uses it, more testable.&lt;/p&gt;

&lt;p&gt;Enjoy, and as always, send complaints this way. :-)&lt;/p&gt;</description>
      <pubDate>Mon, 08 May 2006 23:24:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:25ed7f37-f3d3-40d1-bd85-0650db4141d9</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2006/05/08/jabbering-with-action-messenger</link>
      <category>software</category>
      <category>jabber</category>
      <category>rails</category>
      <category>ruby</category>
      <category>actionmessenger</category>
    </item>
    <item>
      <title>Higher Order Messages for Mapping</title>
      <description>&lt;p&gt;Say you want to convert an array of strings to uppercase. You could write&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map { |name| name.upcase }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/ToProc.rdoc" title="PragDave (Blog)"&gt;PragDave&lt;/a&gt; suggests an alternative which uses the &lt;a href="http://extensions.rubyforge.org/" title="Ruby Extensions Project (Web Site)"&gt;Ruby Extensions Project&lt;/a&gt;&amp;#8217;s Symbol#to_proc method to pass a symbol.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map(&amp;amp;:upcase)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is all well and good, but can you really call it elegant?  Suppose you want to add 1 to all elements in a list.  How can this method let you pass in that parameter?  It seems that it can&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;Higher Order Messages (HOM) provide a better way, although a way which seems even more like magic.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://rubyforge.org/projects/homer" title="Homer: Higher Order Message Extension to Ruby"&gt;Homer&lt;/a&gt; project offers HOM for Ruby, and although it didn&amp;#8217;t have a method which did something like map, it was relatively simple to whip something together.&lt;/p&gt;

&lt;p&gt;My own version of the above functionality:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;result = names.map_using.upcase
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Isn&amp;#8217;t that better?  I couldn&amp;#8217;t call it map without causing a little mayhem, having to alias the old version and still provide complete compatibility was a bit much for me at this point in time.&lt;/p&gt;

&lt;p&gt;But better yet, this version &lt;em&gt;does&lt;/em&gt; permit you to use parameters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;costs_with_gst = costs.map_using * 1.1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don&amp;#8217;t bother asking for the source code for this&amp;#8230; I&amp;#8217;ll be shuffling it up to the Homer project as soon as I hit submit. ;-)&lt;/p&gt;</description>
      <pubDate>Fri, 04 Nov 2005 10:29:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:eebc6dfa-f369-4556-92d9-b28b223bdaa3</guid>
      <author>Trejkaz</author>
      <link>http://trypticon.org/articles/2005/11/04/higher-order-messages-for-mapping</link>
      <category>programming</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
