<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nearly random &#187; ruby</title>
	<atom:link href="http://www.johnbaylor.org/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnbaylor.org</link>
	<description>but with a nefarious purpose</description>
	<lastBuildDate>Thu, 18 Nov 2010 19:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Rails sites: consumer-facing vs. enterprise</title>
		<link>http://www.johnbaylor.org/2010/10/22/rails-sites-consumer-facing-vs-enterprise/</link>
		<comments>http://www.johnbaylor.org/2010/10/22/rails-sites-consumer-facing-vs-enterprise/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 07:04:49 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/?p=85</guid>
		<description><![CDATA[At my current position and the previous one, I&#8217;ve worked on shrink-wrapped Rails application that are meant to be installed onto an enterprise-customer&#8217;s network. For want of a better name, I&#8217;ll call this an enterprise rails app. This is the dark matter of the Rails installations &#8211; we don&#8217;t know how many there are since [...]]]></description>
			<content:encoded><![CDATA[<p>At my current position and the previous one, I&#8217;ve worked on shrink-wrapped Rails application that are meant to be installed onto an enterprise-customer&#8217;s network. For want of a better name, I&#8217;ll call this an enterprise rails app. This is the dark matter of the Rails installations &#8211; we don&#8217;t know how many there are since they are only seen by the enterprise users.  But I think there are a lot of these enterprise applications and I&#8217;m beginning to see some differentiation from the customer-facing sites that we go to every day.</p>
<p>Release Cycle</p>
<p>At a consumer-facing site, you can push small updates on a daily basis. The users see a gradual evolution over time. Contrast this with the enterprise Rails site, where each version goes through months of development before any customers see it &#8211; and its a big jump when they do. Hopefully the big jump is in the right direction &#8211; an improvement &#8211; but its hard to guess right every time.</p>
<p>Development Cycle</p>
<p>The development process for the two types of sites reflects the developer&#8217;s needs. An enterprise app only needs to be perfect once or twice a year, when it is shipped out the door, whereas a consumer-focused site needs to be bug-free every single day for every single user. This leads to radically different development strategies.  For a consumer-facing site, the agile processes of making small fast changes with solid test coverage is about the only way to stay sane &#8211; lack of tests means unexpected late-night debugging while user&#8217;s stare at a fail-whale. Enterprise developers, while they may talk about TDD and agile, have no need to actually practice it since they have months to fix issues that creep in.</p>
<p>Death by a thousand Demos</p>
<p>The final area where this difference in process manifests is in how the business needs mesh (or don&#8217;t) with the development team. As a vendor to an enterprise-level company, we&#8217;re expected to periodically provide a &#8220;demo&#8221; version of the site to display at trade shows. This demo-ware needs to look like the final product and work, at a cursory level, like the final product &#8211; but it doesn&#8217;t need to actually work in a full and complete way. Smoke and mirrors is the name of the game when creating demos.</p>
<p>A consumer-facing site has no demos. What you see is what you get. It either works or it doesn&#8217;t &#8211; you can&#8217;t hide your failures from the user.</p>
<p>Developer Practices</p>
<p>Given the disparity in business requirements, its no wonder there is a huge difference in developer practices. When end-users are always using your site you have little choice but to use best-practices: test everything, keep all the tests passing, leave little development debt. When the end-users won&#8217;t see it for many months its easy to get lazy (I&#8217;ve done it): let the development debt pile up, rely on QA testing since you didn&#8217;t take the time to write automated tests, hope QA will catch all the bugs &#8211; but pay the price in rework and bug fixing long after you thought you were long done with with writing that code.</p>
<p>I&#8217;m curious to see whether this dichotomy will ever change. Maybe it already has somewhere. As one of my favorite authors says: &#8220;The future is already here, its just not evenly distributed&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2010/10/22/rails-sites-consumer-facing-vs-enterprise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scrappy</title>
		<link>http://www.johnbaylor.org/2009/12/10/scrappy/</link>
		<comments>http://www.johnbaylor.org/2009/12/10/scrappy/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 18:55:23 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/?p=75</guid>
		<description><![CDATA[Update: Ignore all the great ideas below. @defunkt pointed me to jaml, which does a big chunk of what I was interested in. I&#8217;ve followed the work of @hcatlin, @defunkt and @justinbmeyer on templating languages (haml, mustache and javascriptMVC) and have looked for something that met my needs. Here is an idea that seems simpler [...]]]></description>
			<content:encoded><![CDATA[<p><em>Update: Ignore all the great ideas below. @defunkt pointed me to <a href="http://github.com/edspencer/jaml/">jaml</a>, which does a big chunk of what I was interested in.</em></p>
<p>I&#8217;ve followed the work of <a href="http://twitter.com/hcatlin">@hcatlin</a>, <a href="http://twitter.com/defunkt">@defunkt</a> and <a href="http://twitter.com/justinbmeyer">@justinbmeyer</a> on templating languages (<a href="http://haml-lang.com/">haml</a>, <a href="http://javascriptmvc.com/">mustache</a> and <a href="http://javascriptmvc.com/">javascriptMVC</a>) and have looked for something that met my needs. Here is an idea that seems simpler than javascriptMVC but has more power than haml or mustache &#8211; hopefully the sweet spot I&#8217;ve been looking for.</p>
<p>The initial idea, inspired by haml but not needing a text parser, was to represent markup as JSON:</p>
<p><script src="http://gist.github.com/253446.js?file=simple_markup.js"></script> Which would generate: <script src="http://gist.github.com/253468.js?file=simple_markup.html"></script></p>
<p>However, this provides no support for interaction with the markup. When considering generating a set of dialogs for user interaction I really wanted field validation built in, like this:</p>
<p><script src="http://gist.github.com/253511.js?file=markup_with_validation.js"></script> Eventually I realized that wrapping all the functionality as a filter, in essence creating a tiny <em>scrap</em> of functionality, was what I was looking for:  <script src="http://gist.github.com/253551.js?file=scrap_for_user_form.js"></script></p>
<p>I could see wrapping a form scrap in a larger dialog-handling scrap that knew enough to validate its internal scraps before allowing the OK button to save the data. It should work &#8211; I just wish it could be as pretty as the others &#8211; but I don&#8217;t see that as possible, given the semantics of JSON.</p>
<p>The true test will be when I actually implement the code to support such a construct. Look for a <em>johnb/scrappy</em> project on Github. Until then, I&#8217;ll just assume it works! <img src='http://www.johnbaylor.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2009/12/10/scrappy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyConf 2009</title>
		<link>http://www.johnbaylor.org/2009/11/30/rubyconf-2009/</link>
		<comments>http://www.johnbaylor.org/2009/11/30/rubyconf-2009/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 18:05:33 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/?p=68</guid>
		<description><![CDATA[I was recently given the opportunity of attending Ruby Conf 2009. It was a lot of fun being with a few hundred of my Rubyist peers. You can now see for yourself via the recently-uploaded session videos or the RubyConf-in-22 minutes video. Or read other RubyConf wrap-ups. Due to the sheer amount of information, my [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently given the opportunity of attending <a href="http://www.rubyconf.org/">Ruby Conf 2009</a>. It was a lot of fun being with a few hundred of my Rubyist peers. You can now see for yourself via the recently-uploaded <a href="http://rubyconf2009.confreaks.com/">session videos</a> or the <a href="http://blog.envylabs.com/2009/12/rubyconf-videos/">RubyConf-in-22 minutes video</a>. Or read <a title="Tim Bray's" href="http://www.tbray.org/ongoing/When/200x/2009/11/21/RubyConf-2009">other</a> RubyConf wrap-ups. Due to the sheer amount of information, my personal wrap-up here is in the boring <em>at-this-time-I-saw-this</em> format. I&#8217;ll try to have links to each talk, but they aren&#8217;t all available at the time of writing this &#8211; I&#8217;ll try to update the list later.</p>
<p>Wednesday night I received my ticket via email from <a href="http://jake.howmeta.com/">a nice guy in New York</a> who couldn&#8217;t go at the last minute. Lucky for me!</p>
<p>Thursday 9am the conference started with a keynote from <a href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto">&#8220;Matz&#8221; (Yukihiro Matsumoto)</a> the creator of Ruby. It was not the most interesting talk I&#8217;d heard, but everyone listened politely. Immediately after Matz&#8217;s talk I picked up my free <a href="http://www.poken.com/about">Poken</a> device to track the people I met. The Poken was kinda fun at the conference but not very useful unless you&#8217;re near a lot of new people who also have them. But at the conference it was kinda fun to touch someone&#8217;s Poken to capture a digital business card.</p>
<p>Thursday 10am I attended the <a href="http://rubyconf2009.confreaks.com/19-nov-2009-10-25-bert-and-ernie-scaling-your-ruby-site-with-erlang-tom-preston-werner.html">BERT and ERNIE</a> talk from one of the GitHub guys, which was fairly close to <a href="http://github.com/blog/531-introducing-bert-and-bert-rpc">what I&#8217;d already read</a> of the new GitHub server infrastructure. Its amazing what you can do with smart people and enough servers to effectively distribute your workload.</p>
<p>Thursday 11am to 4pm I listened to talks on <a href="http://rubyconf2009.confreaks.com/19-nov-2009-11-15-bits-bytes-and-blobs-brian-mitchell.html">Bits and Bytes</a>, <a href="http://rubyconf2009.confreaks.com/19-nov-2009-13-15-dsls-code-generation-and-new-domains-for-ruby-joel-vanderwerf.html">DSLs</a> and Flying Robots &#8211; the quality of these talks was inconsistent and there was not much material I wasn&#8217;t already aware of, but I met some interesting people at each session.</p>
<p>At 4pm Thursday was a <a href="http://www.slideshare.net/mdirolf/mongodb-at-rubyconf?src=related_normal&amp;rel=2557295">talk on MongoDB</a> that cleared up some misconceptions I had. This dovetailed nicely with the Friday 9am &#8220;Not Only SQL&#8221; talk about other non-SQL database options. Unfortunately, I don&#8217;t see any obvious winner over my current use of PostgreSQL &#8211; there probably is a better one for our particular needs but the cost, in time, of trying the various options seems prohibitive.</p>
<p>Thursday 5pm was my last session of the day, with a presentation on Experiment Driven Development. The basic idea is that rather than relying on your best guess, or your Marketing guy&#8217;s best guess about a new feature, you just test both new ideas on actual users before committing to a definite plan of action. He hasn&#8217;t put up any slides for his talk (he has a 1 month old baby at home) so you&#8217;ll have to rely on my judgement that it was interesting. For me, it means that I&#8217;ll be looking for opportunities to capture metrics to measure user response to system changes. I expect the rest of the <a href="http://rubyconf2009.confreaks.com/">ConFreaks&#8217;</a> videos will be online in the next week or two.</p>
<p>Rather than stay for the 8pm <a href="http://blog.hasmanythrough.com/2009/11/20/rubyconf-2009-lightning-talks">Lightning Talks</a> I went with my friend <a href="http://twitter.com/dan">Dan</a> and his friends to a yummy tapas restaurant in the Mission.</p>
<p>Friday I learned more than I wanted to know about ongoing improvements to the RubyGems system, partial evaluation of Ruby (pseudo JIT compilation) and an alpha-level persistence layer called Aqua.</p>
<p>At lunch on Friday (and again on Saturday), I watched a fun programming competition to see which team could refactor a chunk of code better and faster. It was all very subjective, but gave a real-world view into how pair programming actually works and other people&#8217;s ideas on what makes for a consistent test-driven process.</p>
<p>Friday 2pm was a talk on the latest in <a href="http://www.macruby.org/">MacRuby</a>, a very promising Ruby VM running on top of Objective-C using native objects and garbage collection. It looks promising but I won&#8217;t play with it until the 0.5 release is officially out (any day now).</p>
<p>Friday 3pm was Jim Weirich&#8217;s talk on SOLID Ruby that expanded on some ideas about Object Oriented Programming that were <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">originally described by &#8220;Uncle Bob&#8221;</a>. He is a good speaker who really knows his topic so my high expectations were happily met.</p>
<p>Friday 4pm was a fun talk by the always-interesting creator of GitHub, Chris Wanstrath, on what Rubyists can learn from Python.</p>
<p>Friday evening I attended the <a title="Map of Startup Crawl venues" href="http://maps.google.com/maps/ms?ie=UTF8&amp;hl=en&amp;msa=0&amp;msid=116134150482170869797.00047832bca7c11a7cb41&amp;ll=37.790184,-122.412672&amp;spn=0.045309,0.066261&amp;z=14">Startup Crawl</a>, eating tasty food, drinking beer and meeting a few people. <a href="http://www.scribd.com/">Scribd</a> had the best food; <a href="http://heroku.com">Heroku</a> had the best ambiance (and one of their guys solved a technical problem I had on one of my personal sites &#8211; nice bonus &#8211; instead of telling me to <a href="http://docs.heroku.com/rack">RTFM</a>).</p>
<p>Saturday I woke a bit late but made it in time for the <a href="http://clojure.org/">Clojure</a> talk &#8211; it seems like a good language for concurrency, but not nearly as easy to write as Ruby. Tim Bray has a nice <a href="http://www.tbray.org/ongoing/When/200x/2009/09/27/Concur-dot-next">ongoing series of blog posts</a> on various languages that focus on concurrency. He recently <a href="http://www.tbray.org/ongoing/When/200x/2009/10/26/Messaging">delved</a> <a href="http://www.tbray.org/ongoing/When/200x/2009/11/11/Clojure-References">into</a> <a href="http://www.tbray.org/ongoing/When/200x/2009/11/18/Clojure-Parallel-I-O">Clojure</a> and it looks <a href="http://www.tbray.org/ongoing/When/200x/2009/11/25/Clojure-grinding">somewhat</a> promising (but there is <a href="http://www.tbray.org/ongoing/When/200x/2009/11/26/No-Free-Lunch">no free lunch</a>).</p>
<p>Saturday 11am I attended an interesting talk on scaling called  <a href="http://www.pauldix.net/2009/11/resources-for-synchronous-reads-asynchronous-writes-at-rubyconf-2009.html"><em>Synchronous Reads, Asynchronous Writes</em></a>. Paul Dix went on at length on the various strategies his site has used to improve performance by off-loading work to services that allow <em>eventual</em> data consistency. It looks like a fairly standard infrastructure once you get to a large number of servers but it was good to hear the nitty gritty details discussed.</p>
<p>My last session was Gregg Pollack&#8217;s discussion of  a number of performance-related gems and plugins. It was essentially what he covers in his <a href="http://blog.envylabs.com/2009/11/scaling-rails/">screencasts</a>, but since I hadn&#8217;t seen them it was well worth hearing it in person. I&#8217;ve now subscribed to his <a href="http://ruby5.envylabs.com/">Ruby5 podcast </a>which I might finally listen to on the train.</p>
<p>All in all, it was a very good conference. I met, in person, a lot of interesting people that I&#8217;d been following in various ways (twitter, blogs, RSS feeds, etc.) and I&#8217;d love to go again next year &#8211; but I&#8217;ll try to get a ticket <em>when </em>they go on sale instead of the night before!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2009/11/30/rubyconf-2009/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Toothpicks</title>
		<link>http://www.johnbaylor.org/2008/12/21/toothpicks/</link>
		<comments>http://www.johnbaylor.org/2008/12/21/toothpicks/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 19:25:09 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/?p=52</guid>
		<description><![CDATA[Intending to direct a new Ruby-ist to rubyquiz.com, I hastily misread the Counting Toothpicks quiz as the reverse of what was intended.  So here is my quick code to solve the wrong problem: converting a string of toothpicks to its underlying equation and giving the answer. I was quite happy with my short solution until [...]]]></description>
			<content:encoded><![CDATA[<p>Intending to direct a new Ruby-ist to <a href="http://rubyquiz.com/">rubyquiz.com</a>, I hastily  misread the <a href="http://rubyquiz.com/quiz111.html">Counting Toothpicks</a> quiz as the reverse of what was intended.  So here is my quick code to solve the wrong problem: converting a string of toothpicks to its underlying equation and giving the answer. I was quite happy with my short solution until realizing that I had solved a much simpler problem. In any case, here it is:</p>
<pre><span class="comment">#</span>
<span class="comment"># toothpicks.rb</span>
<span class="comment">#</span>
<span class="keyword">def </span><span class="method">toothpicks</span> <span class="ident">str</span>
  <span class="ident">puts</span> <span class="ident">str</span>
  <span class="ident">str</span><span class="punct">.</span><span class="ident">gsub!</span><span class="punct">('</span><span class="string"> </span><span class="punct">','</span><span class="punct">')</span>
  <span class="keyword">raise</span> <span class="punct">'</span><span class="string">Wrong format</span><span class="punct">'</span> <span class="keyword">unless</span> <span class="ident">str</span> <span class="punct">=~</span> <span class="punct">/</span><span class="regex">^(x|<span class="escape">\|</span>|<span class="escape">\+</span>|-)*$</span><span class="punct">/</span>
  <span class="ident">count</span> <span class="punct">=</span> <span class="ident">str</span><span class="punct">.</span><span class="ident">length</span>
  <span class="ident">count</span> <span class="punct">+=</span> <span class="ident">str</span><span class="punct">.</span><span class="ident">scan</span><span class="punct">(/</span><span class="regex">(x|<span class="escape">\+</span>)</span><span class="punct">/).</span><span class="ident">length</span>
  <span class="ident">str</span><span class="punct">.</span><span class="ident">length</span><span class="punct">.</span><span class="ident">downto</span><span class="punct">(</span><span class="number">1</span><span class="punct">)</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">n</span><span class="punct">|</span>
    <span class="ident">str</span><span class="punct">.</span><span class="ident">gsub!</span><span class="punct">(</span> <span class="punct">'</span><span class="string">|</span><span class="punct">'*</span> <span class="ident">n</span><span class="punct">,</span> <span class="ident">n</span><span class="punct">.</span><span class="ident">to_s</span> <span class="punct">)</span>
  <span class="keyword">end</span>
  <span class="ident">str</span><span class="punct">.</span><span class="ident">gsub!</span><span class="punct">('</span><span class="string">x</span><span class="punct">','</span><span class="string">*</span><span class="punct">')</span>
  <span class="ident">puts</span> <span class="ident">str</span>
  <span class="ident">result</span> <span class="punct">=</span> <span class="ident">eval</span><span class="punct">(</span><span class="ident">str</span><span class="punct">)</span>
  <span class="ident">puts</span> <span class="punct">"</span><span class="string"><span class="expr">#{result}</span> from <span class="expr">#{count}</span> toothpicks</span><span class="punct">"</span>
<span class="keyword">end</span>

<span class="keyword">if</span> <span class="constant">ARGV</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span>
  <span class="ident">toothpicks</span> <span class="constant">ARGV</span><span class="punct">[</span><span class="number">0</span><span class="punct">].</span><span class="ident">dup</span>
<span class="keyword">else</span>
  <span class="ident">toothpicks</span> <span class="punct">'</span><span class="string">||| x || + ||||| - |||||||</span><span class="punct">'</span>
<span class="keyword">end</span></pre>
<p>Running with no arguments tests the example equation:</p>
<pre>&gt;&gt;toothpicks.rb
||| x || + ||||| - |||||||
3*2+5-7
4 from 22 toothpicks</pre>
<p>Note: syntax highlighting provided by <a href="http://www.rubyinside.com/advent2006/7-coloring.html">this nice page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2008/12/21/toothpicks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Self-Modifying Code? Or Self-Creating Code?</title>
		<link>http://www.johnbaylor.org/2008/04/20/self-modifying-code-or-self-creating-code/</link>
		<comments>http://www.johnbaylor.org/2008/04/20/self-modifying-code-or-self-creating-code/#comments</comments>
		<pubDate>Sun, 20 Apr 2008 19:49:20 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2008/04/20/self-modifying-code-or-self-creating-code/</guid>
		<description><![CDATA[[aside: Why write an email that will be read by just one or two people when you can instead write a blog post that will be seen by... uh... one or two people?] I was recently discussing the problem of tracing program execution through code that doesn&#8217;t exist.  Sometimes, usually to understand and debug a [...]]]></description>
			<content:encoded><![CDATA[<p>[aside: Why write an email that will be read by just one or two people when you can instead write a blog post that will be seen by... uh... one or two people?]</p>
<p>I was recently discussing the problem of tracing program execution through code that doesn&#8217;t exist.  Sometimes, usually to understand and debug a program, you need to trace through it. If the code doesn&#8217;t exist, its hard to follow where it goes. Actually, the code exists, its just not in source control or otherwise easily searchable.  Let me explain.</p>
<p>In Ruby, whose source code is a lot like its executable format, its trivial to write code that writes code &#8211; so you, the programmer, don&#8217;t have to. The Rails web framework uses this a lot, for creating whatever form of &#8216;find&#8217; strikes your fancy: find_by_firstname, find_by_firstname_and_gender, find_by_pet_species_and_viciousness, etc. &#8211; as long as your database table has the appropriately named columns then it will find what you&#8217;re looking for with no work on your part!  Actually, some work is required &#8211; you need to know that most any routine starting with &#8216;find&#8217; is probably auto-generated by Rails and thus will be un-findable in the source tree.</p>
<p>A more complex example is the routing helpers &#8211; a shorthand way of saying &#8220;put a link here on <em>this </em>web page that will take the user to <em>that</em> web page over there.  Until you realize that most any method ending in &#8216;path&#8217; is a URL helper then you&#8217;ll be confused by code that references user_edit_path or formatted_pet_list_path or formatted_pet_species_list_path.  The last one says that you want to get the path to a specially-formatted list of the various pet species represented by the system (e.g. &#8220;http://myPetSite.com/pet_species/list.csv&#8221;). Fairly clear once you know what it does, but fairly obtuse until you get to that point.  And it is, trust me on this, shorter, more maintainable and more clear than the alternative (after, of course, you learn to read it).</p>
<p>So this difficulty in finding the source to &#8220;formatted_pet_species_list_path&#8221; (or something like that) started a discussion that eventually got around to the idea that Ruby and Rails uses code to write code.</p>
<p>&#8220;Its the 11th commandment&#8221;, said one, &#8221; &#8211; thou shalt not write self-modifying code!&#8221;</p>
<p>Yes and no. Call me irrationally exuberant or say that I drank some sugary flavored colored water &#8211; but I don&#8217;t think its as cut-and-dried as it used to be.  This commandment, when Moses brought it down from on high, was written for a compiled language, where the source was very very different from the executing code, and it actually did <em>modify </em>the code being executed.  This is bad bad bad &#8211; its hard to understand, it leads to intractable bugs, it&#8217;ll make you go blind, melt glaciers and other bad stuff.  No argument there.</p>
<p>But is this what Rails is doing with Ruby? I&#8217;d say its different.  Instead of <em>modifying </em>existing code it is merely <em>creating </em>code that did not exist before. Its more akin to a pre-processor that could, just before compiling a program, generate all the permutations of finding database rows from columns X, Y and Z (and any number of others).  Looked at this way, it appears to be similar to a C++ template &#8211; generic code that a programmer has written to simplify the writing of code that does similar things in similar ways.  The main difference is that with Ruby there is no pre-processor &#8211; the routine gets created at the time it is first used.</p>
<p>The &#8220;template&#8221; in this case is a routine called method_missing that gets handed the name and arguments of any routine that doesn&#8217;t exist.  It looks for a function name matching the form &#8216;find_by_<em>X</em>&#8216; where <em>X</em> makes sense for the database table in question (of course, if it doesn&#8217;t match the correct format then it just passes the name and arguments onward for some other routine to either make sense of or to burp up an error).  Once the routine is created it is available to be called again, with none of the overhead incurred when it was first created. More importantly, it was created with no additional overhead on the part of the programmer &#8211; to write it, test it, debug it or modify it. This, in my opinion, is a huge advantage &#8211; but I&#8217;m a lazy developer who doesn&#8217;t want to write or debug any code that I don&#8217;t have to.</p>
<p>Kool-aid anyone?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2008/04/20/self-modifying-code-or-self-creating-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>unpack!</title>
		<link>http://www.johnbaylor.org/2008/02/27/u/</link>
		<comments>http://www.johnbaylor.org/2008/02/27/u/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 07:54:16 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2008/02/27/u/</guid>
		<description><![CDATA[[Update 10/11/2009: I just found a better tool, bindata, to do what I'm describing in this post. It also lists, at the bottom of the page, many links to yet other implementations of binary data packing/unpacking. Worth checking out if this is what you need.] [Update: presentation from the 4/15/2008 Ruby Meetup is now available here.] I [...]]]></description>
			<content:encoded><![CDATA[<p>[Update 10/11/2009: I just found a better tool, <a href="http://bindata.rubyforge.org/">bindata</a>, to do what I'm describing in this post. It also lists, at the bottom of the page, many links to yet other implementations of binary data packing/unpacking. Worth checking out if this is what you need.]</p>
<p>[Update: presentation from the <a href="http://ruby.meetup.com/81/calendar/7577430/" target="_blank">4/15/2008 Ruby Meetup</a> is now available <a href="http://docs.google.com/Presentation?id=ad9wfpzzrhx_46fnbpqrgx" target="_blank">here</a>.]</p>
<p>I like reading code. Its like a novel and I want to read it cover-to-cover. Some, such as <a title="why not" href="http://en.wikipedia.org/wiki/Why_the_lucky_stiff">Why&#8217;s</a> <a title="The full source code, with many many comments." href="http://github.com/camping/camping/blob/master/lib/camping-unabridged.rb">Camping framework</a>, I struggle to comprehend. But most code that I read comes up slightly short. Like a novel with some mis-spellings, awkward phrasing or repeated analogies, I mentally mark it as &#8220;could be better&#8221;. And sometimes I really do sit down and write something better &#8211; maybe just for my own amusement but often for a useful purpose.</p>
<p>I recently had the experience of reading some code that parsed a variable-length binary data structure. This sort of thing comes up often when parsing a file format or communications protocol. Most of the code looks fairly similar because it does similar stuff: ignore one byte, read the next four as the length of the following junk, read two important bytes, ignore two more, read another four-byte length and skip past the following N bytes &#8211; ad nauseum.</p>
<p>I&#8217;ve written it in C, and it looks something like this (ignoring error conditions like getting to the end of the buffer):</p>
<pre>ptr = &amp;data;                  // start at the beginning of our data
ptr++;                        // skip junk we don't care about
UInt32 len = *(UInt32 *) ptr; // get the 4-byte length
len = ntohl(len);             // convert from network byte ordering
ptr += sizeof(UInt32);        // skip past the length we just read
ptr += len;                   // skip past the data we don't care about
UInt16 cost = *(UInt16 *)ptr; // read our important two bytes
cost = ntohs(cost);           // convert to the correct byte ordering</pre>
<p>In Ruby, this tends to be shorter due to the handy String.unpack() routine, which takes a concise format string to define how many bytes to read and what to do with them. &#8220;a3&#8243; reads 3 bytes as a string, &#8220;N&#8221; reads 4 bytes in network order, &#8220;n&#8221; reads 2 bytes in network order, etc. The code above could be rewritten in Ruby like this:</p>
<pre><span class="ident">array</span> <span class="punct">=</span> <span class="ident">data</span><span class="punct">.</span><span class="ident">unpack</span><span class="punct">(</span> <span class="punct">"</span><span class="string">a1N</span><span class="punct">")</span>        <span class="comment"># read the junk and the 4 length bytes</span>
<span class="ident">len</span> <span class="punct">=</span> <span class="ident">array</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>                     <span class="comment"># only get the length value we care about</span>
<span class="ident">data</span> <span class="punct">=</span> <span class="ident">data</span><span class="punct">[</span><span class="number">5</span><span class="punct">..-</span><span class="number">1</span><span class="punct">]</span>                 <span class="comment"># throw away the stuff we just read</span>
<span class="ident">array</span> <span class="punct">=</span>  <span class="ident">data</span><span class="punct">.</span><span class="ident">unpack</span><span class="punct">(</span> <span class="punct">"</span><span class="string">a<span class="expr">#{len}</span>n</span><span class="punct">"</span> <span class="punct">)</span> <span class="comment"># define the length to read on the fly</span>
<span class="ident">cost</span> <span class="punct">=</span> <span class="ident">array</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>                    <span class="comment"># get our data in its correct ordering</span>
<span class="ident">data</span> <span class="punct">=</span> <span class="ident">data</span><span class="punct">[(</span><span class="ident">len</span><span class="punct">+</span><span class="number">2</span><span class="punct">)..-</span><span class="number">1</span><span class="punct">]</span>           <span class="comment"># again, throw away what we just read</span></pre>
<p>This code works fine, but its not much more readable than the C code. A first step would be do define a string.unpack!() routine, where the &#8216;!&#8217; exclamation clues us in that it modifies the object we&#8217;re working with. In this case, the modification is to <span style="font-style: italic">eat</span> (discard) the data we just read. This shortens the code to:</p>
<pre><span class="ident">array</span> <span class="punct">=</span> <span class="ident">data</span><span class="punct">.</span><span class="ident">unpack!</span><span class="punct">(</span> <span class="punct">"</span><span class="string">a1N</span><span class="punct">")</span>       <span class="comment"># read the junk and the 4 length bytes</span>
<span class="ident">len</span> <span class="punct">=</span> <span class="ident">array</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>                     <span class="comment"># only get the length value we care about</span>
<span class="ident">array</span> <span class="punct">=</span>  <span class="ident">data</span><span class="punct">.</span><span class="ident">unpack!</span><span class="punct">("</span><span class="string">a<span class="expr">#{len}</span>n</span><span class="punct">")</span>  <span class="comment"># define the length to read on the fly</span>
<span class="ident">cost</span> <span class="punct">=</span> <span class="ident">array</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>                    <span class="comment"># get our data in its correct ordering</span></pre>
<p>But again, this isn&#8217;t much more readable (in my opinion) than the C code. Additionally, it doesn&#8217;t help us understand the code much better in the case where our format string is &#8220;a3Nna5&#8243; and we need to remember which item in &#8216;array&#8217; corresponds to the &#8216;n&#8217; in the string (in this case, it is array[2]). After a test iteration or two, what I finally hit upon was to encapsulate the behavior we want in a separare Unpacker class, that automatically eats the data it reads and stores the results in an internal Hash object, to map the name &#8216;len&#8217; or &#8216;cost&#8217; to the data. I also combined the format string and the resulting variable so we can clearly see the relationships. The result looks like this:</p>
<pre><span class="ident">u</span> <span class="punct">=</span> <span class="constant">Unpacker</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">data</span><span class="punct">)</span>
<span class="ident">u</span><span class="punct">.</span><span class="ident">u!</span> <span class="punct">"</span><span class="string">a1        =&gt; unused
      N         =&gt; len</span><span class="punct">"</span>
<span class="ident">u</span><span class="punct">.</span><span class="ident">u!</span> <span class="punct">"</span><span class="string">a<span class="expr">#{u.len}</span> =&gt; unused
      n         =&gt; cost</span><span class="punct">"</span></pre>
<p>Now we can clearly see which values are ignored, which are given meaningful names, and how the format codes relate to the meaning of the data. Changing it to reflect a better understanding of the underlying data will be very easy. Note that the only reason its in two statements is to define a value for u.len before we use it &#8211; blocks of fixed-length data can be one statement.</p>
<p>The code to implement the Unpacker class is only about 30 lines of Ruby &#8211; including the string.unpack!() routine that can be reused separately.</p>
<pre><span class="keyword">class </span><span class="class">String</span>
  <span class="keyword">def </span><span class="method">unpack!</span> <span class="ident">format</span>
     <span class="ident">array</span> <span class="punct">=</span> <span class="constant">self</span><span class="punct">.</span><span class="ident">unpack</span><span class="punct">(</span><span class="ident">format</span><span class="punct">+"</span><span class="string">a*</span><span class="punct">")</span>
    <span class="constant">self</span><span class="punct">.</span><span class="ident">replace</span> <span class="ident">array</span><span class="punct">.</span><span class="ident">pop</span>
     <span class="keyword">return</span> <span class="ident">array</span>
   <span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">class </span><span class="class">Unpacker</span> <span class="punct">&lt;</span> <span class="constant">Hash</span>
   <span class="ident">attr_reader</span> <span class="symbol">:data</span>
 <span class="keyword">def </span><span class="method">initialize</span> <span class="ident">string</span>
     <span class="attribute">@data</span> <span class="punct">=</span> <span class="ident">string</span>
    <span class="keyword">super</span>
  <span class="keyword">end</span>
  <span class="comment"># format string is expected to have whitespace between each</span>
  <span class="comment"># "unpackCode=&gt;variableName" pairing (which can have whitespace</span>
  <span class="comment"># around the "=&gt;").  u! was picked to be short so it would</span>
  <span class="comment"># look nice, and to connote a destructive "unpack!" operation.</span>
  <span class="keyword">def </span><span class="method">u!</span> <span class="ident">format</span>
    <span class="ident">format</span><span class="punct">.</span><span class="ident">gsub</span><span class="punct">(/</span><span class="regex"><span class="escape">\s</span>*=&gt;<span class="escape">\s</span>*</span><span class="punct">/,'</span><span class="string">=&gt;</span><span class="punct">').</span><span class="ident">strip</span><span class="punct">.</span><span class="ident">split</span><span class="punct">(/</span><span class="regex"><span class="escape">\s</span>+</span><span class="punct">/).</span><span class="ident">each</span> <span class="keyword">do</span> <span class="punct">|</span><span class="ident">segment</span><span class="punct">|</span>
    <span class="ident">src</span><span class="punct">,</span><span class="ident">dst</span> <span class="punct">=</span> <span class="ident">segment</span><span class="punct">.</span><span class="ident">split</span><span class="punct">(/</span><span class="regex">=&gt;</span><span class="punct">/)</span>
    <span class="constant">self</span><span class="punct">[</span><span class="ident">dst</span><span class="punct">]</span> <span class="punct">=</span> <span class="attribute">@data</span><span class="punct">.</span><span class="ident">unpack!</span><span class="punct">("</span><span class="string"><span class="expr">#{src}</span></span><span class="punct">")[</span><span class="number">0</span><span class="punct">]</span>
 <span class="keyword">end</span>
<span class="keyword">end</span>
<span class="comment"># Hash_with_Attrs - For the simplicity of using either u.len or u['len'],</span>
<span class="comment"># makes a hash appear to have members for each hash entry. Many thanks</span>
<span class="comment"># to Why_ for collecting this handy routine on his a href= RedHanded blog.</span>
<span class="comment"># Note of Caution: 'len' is fine but 'length' would not be since u.length</span>
<span class="comment"># would give the number of entries in the hash, not the just-parsed value.</span>
<span class="keyword">def </span><span class="method">method_missing</span><span class="punct">(</span><span class="ident">meth</span><span class="punct">,*</span><span class="ident">args</span><span class="punct">)</span>
  <span class="ident">meth</span> <span class="punct">=</span> <span class="ident">meth</span><span class="punct">.</span><span class="ident">id2name</span>
  <span class="keyword">if</span> <span class="ident">meth</span> <span class="punct">=~</span> <span class="punct">/</span><span class="regex">=$</span><span class="punct">/</span>
    <span class="constant">self</span><span class="punct">[</span><span class="ident">meth</span><span class="punct">[</span><span class="number">0</span><span class="punct">..-</span><span class="number">2</span><span class="punct">]]</span> <span class="punct">=</span> <span class="punct">(</span><span class="ident">args</span><span class="punct">.</span><span class="ident">length</span><span class="punct">&lt;</span><span class="number">2</span> <span class="punct">?</span> <span class="ident">args</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span> <span class="punct">:</span> <span class="ident">args</span><span class="punct">)</span>
  <span class="keyword">else</span>
    <span class="constant">self</span><span class="punct">[</span><span class="ident">meth</span><span class="punct">]</span>
  <span class="keyword">end</span>
<span class="keyword">end</span>
<span class="keyword">end</span></pre>
<p>Update: <em>An even cleaner and shorter way would be to implement a <a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language">DSL</a> as a module so the code above could look like this:</em></p>
<pre><span class="ident">a</span> <span class="number">1</span><span class="punct">,</span>    <span class="symbol">:unused</span>
<span class="constant">N</span>       <span class="symbol">:len</span>
<span class="ident">a</span> <span class="symbol">:len</span><span class="punct">,</span> <span class="symbol">:unused</span>
<span class="ident">n</span>       <span class="symbol">:cost</span></pre>
<p><em>(and yes, this is valid Ruby code)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2008/02/27/u/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Xkcd Titles</title>
		<link>http://www.johnbaylor.org/2008/01/14/xkcd-titles/</link>
		<comments>http://www.johnbaylor.org/2008/01/14/xkcd-titles/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 20:26:04 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2008/01/14/xkcd-titles/</guid>
		<description><![CDATA[I&#8217;ve just noticed the geekily hilarious xkcd comic and one of the funniest aspects is that each comic has a &#8216;title&#8217; attribute (the text that pops up when you hover your mouse over the image) that is often as funny as the comic itself. However, the length of the title often causes it to be [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just noticed the <a href="http://xkcd.com/249/" title="Yes, I play blindfold chess; it says so on my resume">geekily</a> <a href="http://xkcd.com/234/" title="You see, when unix gets confused by spaces, you can tell it to ignore the space by "escaping" it with a backslash which is a pun on... oh, never mind">hilarious</a> <a href="http://xkcd.com/" title="Click the link, dummy!">xkcd comic</a> and one of the funniest aspects is that each comic has a &#8216;title&#8217; attribute (the text that pops up when you hover your mouse over the image) that is often as funny as the comic itself.  However, the length of the title often causes it to be truncated in my browser (Firefox 2.x, which probably has an obscure show-entire-title setting).  Rather than arduously do a &#8216;view source&#8217; on each one (or figure out the Firefox setting), I have Ruby do it for me.  And for <em>you</em> if you want:</p>
<pre>
# xkcd.rb
# extract all the titles from xkcd comics since they
# tend to be too long to fully show in the browser

# USAGE: ruby -rubygems -rxkcd.rb -e 'Xkcd.new.show_all'

require 'open-uri'
require 'hpricot'

class Xkcd
  DOMAIN = 'http://xkcd.com/'

  def show id = 343  # 343 is the NSA/RSA one
    begin
      @hp = Hpricot.parse( open( "%s/%d/" % [DOMAIN,id.to_i] ) )
      (@hp / :img).each do |el|
        puts "%4d: %s" % [id.to_i, el[:title]] if el[:title]
      end
    rescue
    end
  end

  def show_all
    0.upto(400) do |i|
      show i
    end
  end
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2008/01/14/xkcd-titles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Iterators &#8211; enough of a reason for Ruby</title>
		<link>http://www.johnbaylor.org/2007/10/11/iterators-enough-of-a-reason-for-ruby/</link>
		<comments>http://www.johnbaylor.org/2007/10/11/iterators-enough-of-a-reason-for-ruby/#comments</comments>
		<pubDate>Thu, 11 Oct 2007 22:09:20 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2007/10/11/iterators-enough-of-a-reason-for-ruby/</guid>
		<description><![CDATA[A non-programmer friend recently asked me why I liked Ruby so much. I asked him for a simple task that I could write in Ruby and we came up with a pyramid &#8211; from a single &#8220;a&#8221; to 26 &#8220;z&#8221;s. So I showed him this one-liner: "a".upto("z") { &#124;c&#124; puts c * (1 + c[0] [...]]]></description>
			<content:encoded><![CDATA[<p>A non-programmer friend recently asked me why I liked Ruby so much.  I asked him for a simple task that I could write in Ruby and we came up with a pyramid &#8211; from a single &#8220;a&#8221; to 26 &#8220;z&#8221;s.  So I showed him this one-liner:</p>
<pre>"a".upto("z") { |c| puts c * (1 + c[0] - "a"[0]) }</pre>
<p>And then showed him the same program in C:</p>
<pre>#include "stdio.h"
int main( int argc, char **argv )
{
  int loop = 0;
  for( loop = 0; loop &lt; 26; loop++ )
  {
    int innerloop = 0;
    for( innerloop = 0; innerloop &lt;= loop; innerloop++ )
    {
      printf( "%c", 'a' + loop );
    }
    printf("n");
  }
  return 0;
}</pre>
<p>Enough said.</p>
<pre>a
bb
ccc
dddd
eeeee
ffffff
ggggggg
hhhhhhhh
iiiiiiiii
jjjjjjjjjj
kkkkkkkkkkk
llllllllllll
mmmmmmmmmmmmm
nnnnnnnnnnnnnn
ooooooooooooooo
pppppppppppppppp
qqqqqqqqqqqqqqqqq
rrrrrrrrrrrrrrrrrr
sssssssssssssssssss
tttttttttttttttttttt
uuuuuuuuuuuuuuuuuuuuu
vvvvvvvvvvvvvvvvvvvvvv
wwwwwwwwwwwwwwwwwwwwwww
xxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyy
zzzzzzzzzzzzzzzzzzzzzzzzzz</pre>
<p><em>2/6/2008 Update: </em>it might be shorter and more clear like this</p>
<pre>("a".."z").each_with_index { |c,i| puts (c * (i + 1)) }</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2007/10/11/iterators-enough-of-a-reason-for-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>d efine for twitter &#8211; step 1</title>
		<link>http://www.johnbaylor.org/2007/09/29/d-efine-for-twitter-step-1/</link>
		<comments>http://www.johnbaylor.org/2007/09/29/d-efine-for-twitter-step-1/#comments</comments>
		<pubDate>Sat, 29 Sep 2007 21:55:33 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2007/09/29/d-efine-for-twitter-step-1/</guid>
		<description><![CDATA[For those of you who use twitter, you&#8217;ll likely recognize the separation of &#8216;d&#8217; from &#8216;efine&#8217; as intentional: &#8216;d&#8217; means direct a message to another twitter user and &#8216;efine&#8217; is the user you&#8217;re sending it to. Together I hope they connote &#8216;define&#8217; because thats what they do. Sending d efine ruby to twitter should, if [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you who use twitter, you&#8217;ll likely recognize the separation of &#8216;d&#8217; from &#8216;efine&#8217; as intentional: &#8216;d&#8217; means direct a message to another twitter user and &#8216;efine&#8217; is the user you&#8217;re sending it to.  Together I hope they connote &#8216;define&#8217; because thats what they do. Sending</p>
<pre>d efine ruby</pre>
<p>to twitter should, if my twitter-bot works as intended, return a direct reply of</p>
<pre>"A clear, deep, red, valued as a precious stone."</pre>
<p>Which is a fairly accurate definition (even if it <em>does </em>leave out my favorite computer language).</p>
<p>So in this part I&#8217;ll describe the definition-grabbing piece, which queries <a href="http://en.wiktionary.org/wiki/java" title="wiktionary.org" target="_blank">wiktionary.org</a> for the first definition.  This first iteration is stupidly simple: read the entire page, parse its contents with the wondrous <a href="http://code.whytheluckystiff.net/hpricot/" title="_why oh _why" target="_blank">Hpricot</a> tool, grab the first item from the first ordered list on the page and throw away any links.  It sometimes gets odd or partial definitions so it will need improvement &#8211; but works great for the five minutes it took to write.</p>
<pre>
require 'open-uri'
require 'hpricot'
def efine word
  open("http://en.wiktionary.org/wiki/#{word}") do |f|
    (Hpricot(f.read) / "ol" / "li")[0].to_plain_text.gsub(/s*[.*]/,'')
  end
end</pre>
<p>That&#8217;s all.  You&#8217;ll have to wait for the twitter-integration piece in my next post.  I haven&#8217;t written it yet, but given the functionality in <a href="http://twitter4r.rubyforge.org/" title="twitter API for Ruby" target="_blank">twitter4r</a>, I doubt it will be much longer than the efine() method above.  In fact, my usual peeve about Ruby is just that: it takes longer to describe the code than to write it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2007/09/29/d-efine-for-twitter-step-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RJS Error: TypeError: $(element) has no properties</title>
		<link>http://www.johnbaylor.org/2007/09/21/rjs-error-typeerror-element-has-no-properties/</link>
		<comments>http://www.johnbaylor.org/2007/09/21/rjs-error-typeerror-element-has-no-properties/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 01:11:34 +0000</pubDate>
		<dc:creator>JohnB</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.johnbaylor.org/2007/09/21/rjs-error-typeerror-element-has-no-properties/</guid>
		<description><![CDATA[I received this error message late yesterday while testing out RJS templates and link_to_remote(). I did a google search and didn&#8217;t find anything useful &#8211; some questions that were asked and never answered; one that said &#8220;rebuild your entire app&#8221;. Finally, I opened the page in another browser and it worked fine. huh? doh! Rails [...]]]></description>
			<content:encoded><![CDATA[<p>I received this error message late yesterday while testing out RJS templates and link_to_remote().  I did a google search and didn&#8217;t find anything useful &#8211; some questions that were asked and never answered; one that said &#8220;rebuild your entire app&#8221;.  Finally, I opened the page in another browser and it worked fine.  huh?</p>
<p>doh!  Rails nearly-seamless simplicity strikes again!</p>
<p>I had just changed my view code (.rhtml file) to include a new div that I wanted to be updated in an AJAXy manner.  So I clicked the link in the browser (remember: this is AJAX &#8211; no page refresh) and expected my new div to be replaced with the neat new content.  Nope.  I had to do a decidedly non-AJAX page refresh so my browser would now <em>have</em> the neat new div &#8211; only then could the div be replaced.</p>
<p>Its so simple to swap between edit and test, edit and test, edit and test, that the few times you&#8217;re required to step out of the cycle seem like a huge hassle.  But not when compared to every <em>other</em>  development process I&#8217;ve used.</p>
<p>I had a similar experience with the routes.rb file.  Unlike models and controllers and non-AJAX views, the routes.rb file only gets loaded when the web server starts.  Stopping and starting the server fixed the problem &#8211; but I think I had to run into it multiple times before I realized what the issue was.  A minor pot-hole on the smooth Rails path.</p>
<p>To mis-quote a bumper sticker: the worst day coding Ruby is better than the best day fighting C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnbaylor.org/2007/09/21/rjs-error-typeerror-element-has-no-properties/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

