<?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>gnuu.org &#187; ruby 1.9</title>
	<atom:link href="http://gnuu.org/tag/ruby-19/feed/" rel="self" type="application/rss+xml" />
	<link>http://gnuu.org</link>
	<description>my word against yours, fight.</description>
	<lastBuildDate>Fri, 16 Jul 2010 22:12:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Announcing The Longest YARD: 0.5.0</title>
		<link>http://gnuu.org/2009/12/14/announcing-the-longest-yard-0-5-0/</link>
		<comments>http://gnuu.org/2009/12/14/announcing-the-longest-yard-0-5-0/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 01:17:12 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[0.5.0]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[yard]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/12/14/announcing-the-longest-yard-0-5-0/</guid>
		<description><![CDATA[YARD 0.5.0 (“The Longest”) was just released today. It features a bunch of new things, but some of the most awesome are: Support for documenting native Ruby C code Incremental file parsing and HTML generation Improved yri tool with support for linking gems Support for Documenting Native Ruby C Code This one was certainly a [...]]]></description>
			<content:encoded><![CDATA[<p>YARD 0.5.0 (<em>“The Longest”</em>) was just released today. It features a bunch of new things, but some of the most awesome are:</p>
<ol>
<li>Support for documenting native Ruby C code </li>
<li>Incremental file parsing and HTML generation </li>
<li>Improved yri tool with support for linking gems </li>
</ol>
<h2>Support for Documenting Native Ruby C Code</h2>
<p>This one was certainly a long time coming. YARD has always planned to support native Ruby code. This was probably the last &quot;big&quot; feature that RDoc could do and YARD could not. Now, YARD can.</p>
<p>The best part of this feature is that YARD can now parse Ruby&#8217;s core codebase and the stdlib, meaning <a href="http://yardoc.org">yardoc.org</a> can now host Ruby-core docs. And it now does: <a href="http://yardoc.org/docs/ruby-core">http://yardoc.org/docs/ruby-core</a>. The stdlib will be added soon.</p>
<h2>Incremental file parsing and HTML generation</h2>
<p>One annoying thing about documenting code with YARD for larger projects is the time it takes to generate HTML. This makes the documentation development cycle slow for previewing small documentation changes. YARD 0.5.0 introduces incremental parsing and HTML generation to parse and generate HTML for only the files that were modified since the last running of <tt>yardoc</tt>, which makes for super-fast previews. To use the incremental parsing feature, simply add <tt>-c</tt> or <tt>--use-cache</tt> and YARD will use the available .yardoc cache.</p>
<pre>$ yardoc --use-cache</pre>
<h2>Improved yri tool with support for linking gems</h2>
<p>The <tt>yri</tt> tool that bundled with YARD prior to this release only worked for the .yardoc file in the local directory. In YARD 0.5.0, <tt>yri</tt> will now perform a lookup on all installed gems on your system. To use this, you&#8217;ll first need to build the .yardoc files for your gems, and you can do this with <tt>yardoc --build-gems</tt>. After that, you can use yri as normal:</p>
<pre>$ yri JSON#load</pre>
<h2>More?</h2>
<p>You can see more new toys in the <a href="http://yardoc.org/docs/yard/file:WhatsNew.md">What’s New</a> document on <a href="http://yardoc.org">yardoc.org</a>. You can also install YARD:</p>
<pre>$ gem install yard</pre>
<p>Enjoy!</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/12/14/announcing-the-longest-yard-0-5-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>YARD 0.4.0 (The Whole Nine) Released Today</title>
		<link>http://gnuu.org/2009/11/15/yard-0-4-0-the-whole-nine/</link>
		<comments>http://gnuu.org/2009/11/15/yard-0-4-0-the-whole-nine/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 18:04:33 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[developer tools]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[yard]]></category>

		<guid isPermaLink="false">http://gnuu.org/?p=317</guid>
		<description><![CDATA[YARD 0.4.0 (codenamed The Whole Nine) was just released today. It’s by far the biggest release since I started writing YARD in 2007. For those who don’t know, YARD is a Ruby documentation tool that surpasses the features of RDoc to bring much richer docs and extensibility to documenteurs. There’s plenty of new features to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://yardoc.org">YARD 0.4.0</a> (codenamed The Whole Nine) was just released today. It’s by far the biggest release since I started writing YARD in 2007. For those who don’t know, YARD is a Ruby documentation tool that surpasses the features of RDoc to bring much richer docs and extensibility to documenteurs. There’s plenty of new features to discuss and plenty of new toys to show off (new templates, live doc server with user comments, new APIs, plugin support), so let’s get started.</p>
<p> </p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/11/15/yard-0-4-0-the-whole-nine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Ruby 1.9, Rails, MySQL and UTF-8 to Work Together</title>
		<link>http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/</link>
		<comments>http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 02:01:33 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[unicode]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/11/06/get-ruby-1-9-rails-mysql-and-utf-8-to-work-together/</guid>
		<description><![CDATA[Update: You can also try out my fork of mysql-ruby that is properly encoding aware. It doesn&#8217;t just convert everything to UTF-8, but rather to your default encoding, so make sure to set your LANG! Here&#8217;s a quick little hack to get MySQL 2.8.1 using UTF-8 in Rails 2.3.4 and Ruby 1.9.1. Filename: lib/mysql_utf8.rb Put [...]]]></description>
			<content:encoded><![CDATA[<p class="note"><strong>Update:</strong> You can also try out my <a href="http://github.com/lsegal/mysql-ruby">fork of mysql-ruby</a> that is properly encoding aware. It doesn&#8217;t just convert everything to UTF-8, but rather to your default encoding, so make sure to <a href="http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/">set your LANG</a>!</p>
<p>Here&#8217;s a quick little hack to get MySQL 2.8.1 using UTF-8 in Rails 2.3.4 and Ruby 1.9.1.</p>
<p class="note">Filename: lib/mysql_utf8.rb</p>
<p><script src="http://gist.github.com/228455.js"></script></p>
<p><!--
<pre class="sh_ruby">class Mysql::Result
  def encode(value, encoding = &#8220;utf-8&#8243;)
    String === value ? value.force_encoding(encoding) : value
  end

  def each_utf8(&#038;block)
    each_orig do |row|
      yield row.map {|col| encode(col) }
    end
  end
  alias each_orig each
  alias each each_utf8

  def each_hash_utf8(&#038;block)
    each_hash_orig do |row|
      row.each {|k, v| row[k] = encode(v) }
      yield(row)
    end
  end
  alias each_hash_orig each_hash
  alias each_hash each_hash_utf8
end</pre>
<p>--></p>
<p>Put that snippet in your Rails project (I used <tt>lib/mysql_utf8.rb</tt>) and load it in your environment.</p>
<p>That's all. Now your queries should use Unicode:</p>
<pre class="sh_ruby">$ ./script/console
Loading development environment (Rails 2.3.4)
&gt;&gt; u = User.find(1)
=&gt; #&lt;User id: 1, name: &quot;Test&quot;, ...&gt;
&gt;&gt; u.name.encoding
=&gt; #&lt;Encoding:ASCII-8BIT&gt;
&gt;&gt; require 'lib/mysql_utf8'
=&gt; []
&gt;&gt; u = User.find(1)
=&gt; #&lt;User id: 1, name: &quot;Test&quot;, ...&gt;
&gt;&gt; u.name.encoding
=&gt; #&lt;Encoding:UTF-8&gt;</pre>
<p>Note that if you have BLOB types in MySQL you’ll need to force_encoding back to another type. You could do this in your model.</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9 Encoding Issues, Again.</title>
		<link>http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/</link>
		<comments>http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 04:45:39 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/</guid>
		<description><![CDATA[I covered Ruby 1.9 encodings a while back on my blog, but apparently I left out a few other major issues. I noticed these just recently, when running 1.9 on a new environment. It turns out, tweaking your environment is all it takes to fix most of the issues, and I had already done this [...]]]></description>
			<content:encoded><![CDATA[<p>I covered Ruby 1.9 encodings <a href="http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/">a while back</a> on my blog, but apparently I left out a few other major issues. I noticed these just recently, when running 1.9 on a new environment. It turns out, tweaking your environment is all it takes to fix most of the issues, and I had already done this on my main machine.</p>
<p>So here we go, the issue:</p>
<h3>You’re using Rails, and you see this:</h3>
<blockquote><p>invalid byte sequence in US-ASCII</p>
</blockquote>
<p>You might also see:</p>
<blockquote><p>incompatible character encodings: ASCII-8BIT and UTF-8</p>
</blockquote>
<p>That happens <em>all the time</em> when trying to get <a href="http://yard.soen.ca">YARD</a> to parse Rails source. It also happens when your ERB templates and your DB data have mismatched encodings.</p>
<h3>The Problem</h3>
<p> Ruby has multiple default encoding values: internal (the default encoding for new String objects), external (the default encoding for file data), and script (the default encoding of the content of Ruby scripts). When dealing with IO, you need to make sure both your internal and external encodings match up, especially since Ruby defaults to ASCII-7BIT and not UTF-8. The “script” encoding problems were covered in my <a href="http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/">last blog post</a> on the subject.&#160;<br />
<h3>The Fix</h3>
<p>All you need to do is tweak your environment variables (your region / language may differ):</p>
<pre>$ export LANG=en_US.UTF-8</pre>
<p>Now Ruby will use UTF-8 for your external encodings: </p>
<pre>$ LANG=en_US.ASCII-7BIT irb19
&gt;&gt; __ENCODING__
=&gt; #&lt;Encoding:US-ASCII&gt;
$ LANG=en_US.UTF-8 irb19
&gt;&gt; __ENCODING__
=&gt; #&lt;Encoding:UTF-8&gt;</pre>
<p>Note, this doesn’t cover your default_internal, but usually this will be handled for you. And if you can’t set your ENV, you can set this stuff right in Ruby:</p>
<pre class="sh_ruby">Encoding.default_internal, Encoding.default_external = ['utf-8'] * 2</pre>
<p>This sets both your default internal and external encodings. </p>
<p>Now when Rails (well, ERB) tries to read files on disk, it will default to UTF-8 rather than ASCII, and your UTF-8 data from the DB will work just fine.</p>
<p>If you have this problem the other way around (your DB is ASCII), just reverse everything I said.</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/11/02/ruby-1-9-encoding-issues-again/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Metaprogramming Ruby, Pt. 1: Markaby</title>
		<link>http://gnuu.org/2009/07/18/metaprogramming-ruby-pt-1-markaby/</link>
		<comments>http://gnuu.org/2009/07/18/metaprogramming-ruby-pt-1-markaby/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 01:40:35 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/07/18/metaprogramming-ruby-pt-1-markaby/</guid>
		<description><![CDATA[With the recent beta release (books can be beta now?) of Metaprogramming Ruby by Paolo Perrotta, I figured I&#8217;d share some of my personal knowledge and tricks in the field of meta-programming. Hopefully I can make this a multi-part series of articles on the subject. In this first article I&#8217;m going to implement a standard [...]]]></description>
			<content:encoded><![CDATA[<p>With the recent beta release (books can be beta now?) of <a href="http://pragprog.com/titles/ppmetr/metaprogramming-ruby">Metaprogramming Ruby</a> by Paolo Perrotta, I figured I&#8217;d share some of my personal knowledge and tricks in the field of meta-programming. Hopefully I can make this a multi-part series of articles on the subject.</p>
<p>In this first article I&#8217;m going to implement a standard Markaby-style DSL that takes declarative Ruby method calls and translates them into HTML. The example only really highlights one metaprogramming trick, namely passing a block to <code>instance_eval</code> to have code executed in the context of an arbitrary object. The rest is just fluff, so it should be simple enough to follow. </p>
<p></p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/07/18/metaprogramming-ruby-pt-1-markaby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Decorators in Ruby Continued</title>
		<link>http://gnuu.org/2009/07/15/python-decorators-in-ruby-continued/</link>
		<comments>http://gnuu.org/2009/07/15/python-decorators-in-ruby-continued/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 15:12:00 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[decorators]]></category>
		<category><![CDATA[force_bind]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[UnboundMethod]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/07/15/python-decorators-in-ruby-continued/</guid>
		<description><![CDATA[I came across Yehuda Katz&#8217;s post about implementing Python-style decorators in Ruby and it certainly piqued my curiousity. I&#8217;m aware of the technical feasibility of implementing this in Ruby (and how completely trivial it is), but what interested me the most is making it look as much like Python as possible, syntax-wise. The fun part [...]]]></description>
			<content:encoded><![CDATA[<p>I came across Yehuda Katz&#8217;s post about <a href="http://yehudakatz.com/2009/07/11/python-decorators-in-ruby">implementing Python-style decorators</a> in Ruby and it certainly piqued my curiousity. I&#8217;m aware of the technical feasibility of implementing this in Ruby (and how completely trivial it is), but what interested me the most is making it look as much like Python as possible, syntax-wise. The fun part here is that Ruby actually has a <code>@style</code> syntax; they&#8217;re instance variables, and you&#8217;re all about to see the horrible abuse I&#8217;ve put them through to get as close to Python decorator syntax as I believe is possible.</p>
<p></p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/07/15/python-decorators-in-ruby-continued/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Git.tmbundle working with Ruby1.9</title>
		<link>http://gnuu.org/2009/05/25/getting-gittmbundle-working-with-ruby19/</link>
		<comments>http://gnuu.org/2009/05/25/getting-gittmbundle-working-with-ruby19/#comments</comments>
		<pubDate>Tue, 26 May 2009 03:03:10 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[bundles]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[textmate]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/05/25/getting-gittmbundle-working-with-ruby19/</guid>
		<description><![CDATA[Tim Harper has a great Git TextMate bundle that I use extensively, but since I’ve converted to Ruby 1.9, I’ve had some problems that others might have run into. Here’s what I did to get everything working nicely in Ruby 1.9: First off, if you haven’t already, you’ll probably want to pull an updated version [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tim.theenchanter.com">Tim Harper</a> has a great Git TextMate bundle that I use extensively, but since I’ve converted to Ruby 1.9, I’ve had some problems that others might have run into. Here’s what I did to get everything working nicely in Ruby 1.9:</p>
<p>First off, if you haven’t already, you’ll probably want to pull an updated version of TextMate’s support files from the svn repository to get some necessary 1.9 compatibility fixes.</p>
<pre>$ cd ~/Library/Application\ Support/TextMate/
$ svn co <a href="http://macromates.com/svn/Bundles/trunk/Support">http://macromates.com/svn/Bundles/trunk/Support</a></pre>
<p>You’ll need to clone <a href="http://github.com/lsegal/git-tmbundle/tree/master">my Git.tmbundle repository</a> into the Bundles directory to get most of the changes I made to his bundle to deal with the basic Ruby 1.9 issues. </p>
<p>After that, you should get most of the basic functionality of the bundle, but you’ll probably notice that if you try to compare branches, revisions, or any of the more fun stuff, you’ll get the following error:</p>
<pre>ArgumentError: An object in the argument tree could not be converted
&#160;&#160;&#160; from (irb):17:in `to_plist'
&#160;&#160;&#160; from (irb):17
&#160;&#160;&#160; from /usr/local/bin/irb:12:in `&lt;main&gt;'</pre>
<p>This is because TextMate ships with a native plist library in its support directory (the one we checked out above) that isn’t quite Ruby 1.9 compatible. We’re lucky though, because an updated version of the library, <a href="http://github.com/kballard/osx-plist">osx-plist</a> (written by Kevin Ballard) is on GitHub, so let’s pull and build it.</p>
<pre>$ cd ~/Library/Application\ Support/TextMate/Support/lib/osx
$ git clone git://github.com/kballard/osx-plist.git
$ cd osx-plist/ext/plist
$ ruby extconf.rb &amp;&amp; make
$ cp plist.bundle ../../../</pre>
<p>Assuming you have TextMate using Ruby 1.9, you should now be able to compare revisions and branches, and do pretty much anything with the awesome Git.tmbundle!</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/05/25/getting-gittmbundle-working-with-ruby19/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Demystifying Continuations in Ruby (because they shouldn&#8217;t be feared)</title>
		<link>http://gnuu.org/2009/03/21/demystifying-continuations-in-ruby/</link>
		<comments>http://gnuu.org/2009/03/21/demystifying-continuations-in-ruby/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 18:35:03 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[continuations]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/03/21/demystifying-continuations-in-ruby/</guid>
		<description><![CDATA[So most people won’t touch continuations with a 10 foot pole, that’s probably why in Ruby 1.9 they’ve been pseudo-deprecated (YARV performance was also an issue here, to be fair) by being pushed out to an explicit “require ‘continuation’” call. Perhaps it’s been misunderstood, but there’s a secret that nobody knows about continuations: they’re dead simple. BRAINDEAD simple.]]></description>
			<content:encoded><![CDATA[<p>So most people won’t touch continuations with a 10 foot pole, that’s probably why they&#8217;ve been <a href="http://wiki.jruby.org/wiki/Differences_between_mri_and_jruby">left out of JRuby</a> and <a href="http://www.atdot.net/~ko1/pub/ContinuationFest-ruby.pdf">pseudo-deprecated</a> in Ruby 1.9 (YARV performance was also an issue here, to be fair) by being pushed out to an explicit “<em>require ‘continuation’</em>” call. Perhaps it’s been misunderstood, but there’s a secret that nobody knows about continuations: <strong>they’re dead simple</strong>. <em>BRAINDEAD</em> simple.</p>
<p>Most blogs and articles I’ve seen that try to explain continuations get it wrong. They bore you with low level details that confuse you in the first place, or scare you by starting with the premise that “it’s complicated, but I&#8217;ll try to make you understand”. I’m not going to do either, I’m actually going to explain continuations in two words, and I want you not to laugh, cry, or vomit on yourself when you read them:</p>
<h2>GLORIFIED GOTOs</h2>
<p>Oh no, I just said a dirty word, didn&#8217;t I. You may have your own irrational fears of the goto statement, but you probably know what they are and how they work:</p>
<pre class="sh_c">#include &lt;stdio.h&gt;
int main() {
  int i = 0;
  printf("%d\n", i);
  goto some_label;
  i = 1;
  some_label:
  printf("%d\n", i);
  return 0;
}</pre>
<p>The above snippet of working C shows how we can jump to different points in a function. It prints 0 twice instead of 0 followed by 1, as you would expect. Language evolution has mostly deprecated this form of programming through method calls and, more intricately, exception handling. These new practices are really just syntactic sugar on the old goto concept. Hopefully all those who scoff at any mention of goto will one day sit down and realize that they&#8217;re a central part of how computers work, but I&#8217;m getting off topic. The point is, whether you use gotos or not, they&#8217;re extremely simple to understand. In fact, they’re exactly as simple as continuations; let’s re-write the above using those &#8220;scary&#8221; guys:</p>
<pre class="sh_ruby">def main
  i = 0
  callcc do |label| # callcc gives us ‘label’, a continuation object
    puts i
    label.call # this is our "goto" statement
    i = 1      # we skip this completely
  end          # this is where our "label" would otherwise sit
  puts i
end</pre>
<p>To those in the know, this doesn&#8217;t really show off the full-extent of callcc&#8217;s powers (that comes later), but you can look at &quot;label.call&quot; as the equivalent of &quot;goto label&quot;, and the end of the callcc block is where the label would be. That&#8217;s basically how continuations work.</p>
<p><em>If you’re having trouble parsing the above: the little trick to understanding the code is simply realizing that the block that callcc yields is <strong>executed immediately, once, and only once</strong>. It’s simply there so you can do any one-time initialization with the continuation you’re creating. The end of the block is where any subsequent calls to the yielded continuation object will go, not the block itself.</em></p>
<h3>NOW YOU KNOW.</h3>
<p>So what, is it really that simple? Continuations are another way to write gotos? The answer is: pretty much. There are actually only two functional difference that makes them even more powerful than the above C-style gotos, but one that makes them a little less-so.</p>
<h4>More Powerful, How?</h4>
<p><span style="font-size: 140%">1.</span> <strong>They’re not local to your method</strong>. Simply put, we can’t do the following in C:</p>
<pre class="sh_c">// a() is called by main()
void a() {
  printf("hello world\n");
  label1:
  printf("then you say...\n");
  b();
}
void b() {
  printf("then I say...\n");
  goto label1;
}</pre>
<p>This should print &quot;hello world&quot; followed by &quot;then you say&quot;, &quot;then I say&quot;, in a never-ending loop. Problem is, it won&#8217;t compile. Without getting into the nitty-gritty of why C can’t do this, I’ll just simply show how we can actually get away with it using continuations:</p>
<pre class="sh_ruby">def a
  puts "hello world"
  callcc {|cc| $label1 = cc } # pretend this says "label1:"
  puts "then you say..."
  b
end

def b
  puts "then I say"
  $label1.call # pretend this says "goto label1"
end</pre>
<p>As the comments indicate, this is almost a one-to-one translation of the C code above. Our continuation object is set to a global variable, essentially making it a “<em>global label</em>” that can be goto’d by running the method #call (picture it as “label.goto” instead of #call).</p>
<p><span style="font-size: 140%">2.</span> <strong>They maintain stack-frame state</strong>. That’s the reason why labels in C are local to a function, because you can’t just jump between functions without switching stack frames. Actually, there are tricks in C to do just that (namely <tt>longjmp</tt> and <tt>setjmp</tt> to jump to arbitrary addresses and continue execution which is actually how continuations are done in C), but we’re focusing on Ruby here, right? Basically, the continuation object yielded from callcc (the {|cc| thing}) contains a snapshot of our stack frame at the point it was yielded at (remember, the block is only yielded that first time). This allows us to jump between methods in a class, between classes, and jump back from a nested call when we are arbitrarily deep in a stack frame. </p>
<h4>Anyone Up For A Quick Real World Example?</h4>
<p>The last situation is the one with some real world usage. Imagine we’re writing a web framework with a Rails-style before_filter and we want the ability to halt the execution during a filter and jump all the way back up to the router code to find the next matching route. <a href="http://www.sinatrarb.com">Sinatra</a> (sort-of) does this with the `pass` method, and though it probably doesn’t use continuations to achieve the result, the concept here is a perfect example of wanting to jump back through our stack frames arbitrarily. We could be 3 method calls deep, or we could be 20 method calls deep, but we need to “go to” a specific point in our program execution. Usually, people implement this with try/catch style exception handling (RouteAbortError maybe), but continuations may be a little cleaner depending on the scenario (we might have a method handling the exception 3 method calls up, but we want to jump 7 method calls up to the initial point in code).</p>
<h4>Less Powerful, How?</h4>
<p>There’s one thing you may have noticed from the initial example, it’s that jumping forward looks different from the example where we jumped backwards. The limitation is that continuations can’t actually jump <em>forwards</em>, at least not between method calls. The reason is that in C, labels are compiled into the program statically, but in Ruby, the continuation objects are created at runtime. This means that we’d need to execute that callcc call in our future method to generate the continuation, but we can’t run code that hasn’t yet been run. In short, continuations are great for jumping back in time, not quite so for jumping forwards.</p>
<h3>Final Correction</h3>
<p>So I initially called continuations “glorified gotos”. Hopefully this got you to understand the simple concept behind their use; but now I should fess up and modify the definition just a little bit to be slightly more accurate. Instead of glorified gotos, think of continuations as:</p>
<h2>GLORIFIED, <u>STATEFUL, BACKWARDS JUMPING,</u> GOTOs</h2>
<p>Now that you know what the fuss is about, you can decide to hate continuations all you want.</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/03/21/demystifying-continuations-in-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9 Common Problems Pt. 1: Encoding</title>
		<link>http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/</link>
		<comments>http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 06:05:41 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[BlueCloth]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/</guid>
		<description><![CDATA[If you’re migrating from Ruby 1.8.x to 1.9 you probably have run into one of the following error messages: invalid multibyte char (US-ASCII) - OR - CompatibilityError: incompatible encoding regexp match (Windows-31J regexp with UTF-8 string) The errors themselves are relatively self-explanatory. Ruby 1.9 is far more Unicode aware than 1.8, and this error happens [...]]]></description>
			<content:encoded><![CDATA[<p>If you’re migrating from Ruby 1.8.x to 1.9 you probably have run into one of the following error messages:</p>
<p><code class="dark">invalid multibyte char (US-ASCII)</code></p>
<p>- OR -</p>
<p><code class="dark">CompatibilityError: incompatible encoding regexp match (Windows-31J regexp with UTF-8 string)</code></p>
<p>The errors themselves are relatively self-explanatory. Ruby 1.9 is far more Unicode aware than 1.8, and this error happens when have some Unicode (usually UTF-8) in one of your files. What you have to do to fix these, however, is not always as straightforward.</p>
<p>Well, after some time pulling my hair out, I&#8217;ve figure out that the solutions to these issues are actually quite simple. </p>
<h3>Invalid multibyte char (encoding here)</h3>
<p>If you get this issue, add the following to the top of each exploding file (below the <a href="http://en.wikipedia.org/wiki/Shebang_(Unix)">shebang</a> if there is one):</p>
<pre class="dark"># encoding: utf-8</pre>
<p style="border-right: #ccc 1px solid; padding-right: 7px; border-top: #ccc 1px solid; padding-left: 7px; font-size: 0.8em; background: #eee; padding-bottom: 7px; margin: 7px; border-left: #ccc 1px solid; padding-top: 7px; border-bottom: #ccc 1px solid"><strong>Note:</strong> You can also use &quot;coding:&quot; or even the Emacs style <tt>-*- encoding: utf-8 -*-</tt> but I like the simple term, &#8216;encoding&#8217;. Also note that you might need to replace &#8216;utf-8&#8242; wth your specific encoding if it&#8217;s something else.</p>
<p>This should resolve the issue.</p>
<p>Basically, Ruby by default assumes that every file is encoded as US-ASCII, and so when it reads UTF-8 (or any Unicode) it freaks out because it is beyond the 7-bit encoding. You have to tell it that the file is encoded as utf-8 by listing it as we did at the top of the file. Yes, it&#8217;s a little anal, but I&#8217;m sure we&#8217;ll get used to it.</p>
<h3>Incompatible encoding regexp match</h3>
<p>This issue is a little bit hairier. I had this problem with the <a href="http://www.deveiate.org/projects/BlueCloth/">BlueCloth</a> 1.0.0 gem (on line 972 of bluecloth.rb). It turns out that there are a <a href="http://www.zenspider.com/Languages/Ruby/QuickRef.html#11">few switches</a> that turn on specific encodings, and for some reason BlueCloth turns on the <tt>//s</tt> switch which enables the SJIS encoding (maybe <a href="http://daringfireball.net/">John Gruber</a> wanted <tt>//m</tt> for multiline?). Ruby 1.8 didn&#8217;t mind having this on, but 1.9 freaks out. Moral of the story, when you see this error, check your Regexp switches.</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/02/02/ruby-19-common-problems-pt-1-encoding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby 1.9.1 Favorite New Features</title>
		<link>http://gnuu.org/2009/01/31/ruby-191-favorite-new-features/</link>
		<comments>http://gnuu.org/2009/01/31/ruby-191-favorite-new-features/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 21:35:43 +0000</pubDate>
		<dc:creator>Loren Segal</dc:creator>
				<category><![CDATA[post]]></category>
		<category><![CDATA[enumerations]]></category>
		<category><![CDATA[new features]]></category>
		<category><![CDATA[ordered hash]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby 1.9]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://gnuu.org/2009/01/31/ruby-191-favorite-new-features/</guid>
		<description><![CDATA[I just grabbed Ruby 1.9.1 today (it was released January 30th 2009, so I’m 24 hours late) and decided to play with it. I’m interested in the changes they’ve been making to the parser, something I’m particularly interested in given my Ruby documentation tool, YARD, is highly dependent on syntax (and I’m messing with the [...]]]></description>
			<content:encoded><![CDATA[<p>I just grabbed <a href="http://ruby-lang.org">Ruby 1.9.1</a> today (it was released January 30th 2009, so I’m 24 hours late) and decided to play with it. I’m interested in the changes they’ve been making to the parser, something I’m particularly interested in given my Ruby documentation tool, <a href="http://github.com/lsegal/yard">YARD</a>, is highly dependent on syntax (and I’m messing with the parser), but some of the other changes are quite nice too. I came across this nice <a href="http://www.scribd.com/doc/2589469/Migrating-to-Ruby-19">migration guide</a> that covers some of the basic changes, but here is a more distilled list of my favorites:</p>
<h3>1. Hash entries are <em>Ordered!</em></h3>
<pre class="sh_ruby">h = { cat: 1, zebra: 2, dog: 3 }
h.delete(:zebra)
h[:monkey] = 4
h.each {|key, val| puts "#{key}: #{val}" }

# Guaranteed to print:
# cat: 1
# dog: 3
# monkey: 4</pre>
<p>I’ve been praying for this for who knows how long. Everyone knows hashes don’t maintain order internally, but much of Ruby development makes use of Hashes as dictionaries because of the simplistic syntax. The problem is that dictionaries, unlike straight hash objects, usually require order to be maintained. There are tons of places in YARD where I had to resort to nested Arrays (eg. <tt>[[key, val], ...]</tt>) to maintain order of a set of associated objects… I’ve even written countless <em>OrderedHash</em> classes to solve this problem. This is definitely the biggest feature in 1.9 to me.</p>
<p><strike>PS. You might have noticed the Pythonish <tt>{a: 1}</tt> syntax— yep, that&#8217;s new too, but it&#8217;s not quite cool enough to make it&#8217;s own item in our list but it’s really convenient to type.</strike></p>
<h3>2. Symbols are now Intuitively Comparable</h3>
<p>YARD calls #to_s on about a million things. Last I checked, Rails does too. This is because internally we try to keep everything stored as a Symbol for obvious efficiency. The problem is that when writing API&#8217;s we&#8217;re usually allowing developers to pass in a String <em>or</em> a Symbol, which means comparison is <em>always</em> annoying. It looks like the Ruby devs looked at a lot of this code, puked all over their keyboards, and cleaned up both their keyboards and the code to deal with this. We can now do things like:</p>
<pre class="sh_ruby">:hello =~ /e/      # => 1
:hello === "hello" # => true
:hello[1] == "e"   # => true</pre>
<h3>3. Enumerations on a Hash return a Hash</h3>
<p>For the same reasons as above, this is awesome. Performing a #select on a hash was always a pain in Ruby 1.8 because you would end up with an Array… totally not a Hash. This inconsistency has been dealt with and we can now do:</p>
<pre class="sh_ruby">{a:1,b:2,c:3}.reject {|k,v| k == :b }
=> {:a=>1, :c=>3}</pre>
<h3>4. Proper Unicode Support</h3>
<p>Let&#8217;s not get too far into this list without giving credit for the awesome work Ruby 1.9 has done in fixing the Unicode support. In short, it is now possible to do things like:</p>
<pre class="sh_ruby">"Hi!".encode("utf-16be") # => "\x00H\x00i\x00!"
File.read('test.txt', encoding: 'utf-8').encoding</pre>
<p>We also get Enumerations like <tt>#each_char</tt> that iterate properly over such strings. There’s plenty more goodies regarding Unicode, but I still don’t know all of the details. Interestingly enough, I ran into a Ruby Unicode problem the other day, so I think I’ll be revisiting the problem with these new tools soon and write about what happened.</p>
<h3>5. Regexps get Look-ahead/Look-behind</h3>
<p>Probably the most powerful feature of regular expressions have been missing from Ruby for the longest time. I can’t remember any specific times when I hit this limitation, but I imagine it will simplify a lot of hacky code attempting to work around the feature omission in 1.8. </p>
<h3>6. Object #tap</h3>
<p>This is an easy one because anyone could have implemented it, but it&#8217;s a nice way to silently hook into a method call chain. The <a href="http://www.ruby-doc.org/core-1.9/classes/Object.html#M000309">Ruby 1.9 docs</a> give a cool example of this, but I think a better example is when dealing with method calls that aren&#8217;t chainable like <tt>Hash#delete</tt>: </p>
<pre class="sh_ruby"># In 1.8 we need to do this because #delete returns nil:
h = {:a => 1, :b => 2, :c => 3}
h.delete(:b)
h.each {|k,v| puts k }

# In 1.9 we can do this in one line:
{a:1,b:2,c:3}.tap {|o| o.delete(:b) }.each {|k,v| puts k }</pre>
<h3>7. Fibers are lighter Threads</h3>
<p>Still have yet to dig into this one, but <a href="http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html">Dave Thomas</a> covers the new Fiber class. In short, they&#8217;re basically lambdas encapsulated in threads— but of course it’s not the implementation but the way you use these guys that makes them quite elegant. I can think of a few things right off the top of my head that involve the quick creation of threads from inline storable procedures.</p>
<h3>8. New Hash Key Syntax</h3>
<p><small>UPDATE:</small> Okay, I wasn&#8217;t going to put this in the list until I realized how the syntax can be applied to method calls. Consider the following 1.8 code:</p>
<pre class="sh_ruby">def open(filename, opts = {}) end

open('test.txt', :access => :read, :close => true)</pre>
<p>The same code in 1.9 can be called as:</p>
<pre class="sh_ruby">def open(filename, opts = {}) end

open('test.txt', access: :read, close: true)</pre>
<p>That <em>is</em> cool.</p>]]></content:encoded>
			<wfw:commentRss>http://gnuu.org/2009/01/31/ruby-191-favorite-new-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
