<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - What&apos;s to Like in Ruby 1.9</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4897/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/04/29/whats-to-like-in-ruby-1-9" rel="alternate" type="text/html"/>

  <updated>2008-08-17T04:14:39-04:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:e5217bec-0197-4165-9741-0d63197e4dda</id>

    <published>2008-04-29T09:52:04-04:00</published>
    <updated>2008-04-29T09:52:04-04:00</updated>
    <title type="html">What&apos;s to Like in Ruby 1.9</title>
    <link href="http://paulbarry.com/articles/2008/04/29/whats-to-like-in-ruby-1-9" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;A few days ago, Charles Nutter asserted that &lt;a href=&quot;http://headius.blogspot.com/2008/04/promise-and-peril-for-alternative-ruby.html&quot;&gt;&quot;people are not flocking to Ruby 1.9 in droves&quot;&lt;/a&gt;.  I would consider myself one of those people.  When I thought about Ruby 1.9, I just assumed they were tweaking the syntax a little but mostly working on a VM that would make Ruby faster.  Mainly though, Ruby 1.9 is an unstable, development release, so it&apos;s really just for playing around with it.  The intent is for 1.9 to drive development for Ruby 2.0, and have people flock to Ruby 2.0 once that is ready for prime time.&lt;/p&gt;

&lt;p&gt;Another factor is that up until today, I didn&apos;t really know much about what is planning on being added to Ruby 1.9.  There is &lt;a href=&quot;http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9&quot;&gt;a complete list of changes in Ruby 1.9&lt;/a&gt;, but it wasn&apos;t until I came across &lt;a href=&quot;http://codefluency.com/articles/2008/04/13/migrating-to-ruby-1-9/&quot;&gt;a slideshow by Bruce Williams that covers some of the changes in Ruby 1.9&lt;/a&gt; that I took the time to download, install and try out a few new features.  So I thought I would put together a blog entry of 5 of my favorite changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;New Literal Syntax for Hashes&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{:foo =&amp;gt; 1, :bar =&amp;gt; &quot;two&quot;}&lt;/code&gt; now becomes &lt;code&gt;{foo: 1, bar: &quot;two&quot;}&lt;/code&gt;.  This is probably minor, but I really like it.  It&apos;s a little bit cleaner, although I&apos;m sure &lt;a href=&quot;http://obiefernandez.com/&quot;&gt;Obie&lt;/a&gt; will be sad to see the &lt;a href=&quot;http://www.hashrocket.com/&quot;&gt;hash rocket&lt;/a&gt; go.  Actually, I&apos;m sure the hash rocket will still be supported, it will just become no longer idiomatic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New Proc literal&lt;/p&gt;

&lt;p&gt;But fear not Obie, we lose one operator, the hash rocket, but gain another, the proc rocket.  You can now define a proc like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;double = -&amp;gt;(x) { x * 2 }
puts double.(5) # =&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That can lead to some pretty crazy things, like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;p = -&amp;gt;(n, name=&quot;it&quot;, &amp;amp;block) {
    n.times{|i| block.call }
    puts &quot;I did #{it} #{n} times&quot;
}


p.(3) do |i| 
  puts &quot;#{i} is #{i.even? ? &apos;not &apos;: &apos;&apos;}odd&quot;
end


p.(3, &quot;foo&quot;) do |i| 
  puts &quot;#{i} is #{i.even? ? &apos;not &apos;: &apos;&apos;}odd&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The difference being that you can now have default values for arguments to a Proc and also you can pass a Block to a Proc.  I could see this being used in metaprogramming.  You could probably do a lot of things with this instead of things that get done using class_eval now.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#Ruby 1.8
class Foo
end
Foo.class_eval &amp;lt;&amp;lt;-CODE
  def bar(x,y=2)
    puts &quot;x =&amp;gt; \#{x}, y =&amp;gt; \#{y}&quot;
  end
CODE      


#Ruby 1.9
class Foo
end
Foo.send(:define_method, :bar, -&amp;gt;(x, y=2) { 
  puts &quot;x =&amp;gt; #{x}, y =&amp;gt; #{y}&quot; 
})
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not sure yet if that would be better, I guess we&apos;ll see.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hashes preserve insert order&lt;/p&gt;

&lt;p&gt;Say goodbye to using 2D arrays just because you want things to be in a certain order.  This is very convenient, but I was always under the impression that this is the way it was for performance reasons.  I&apos;d like to hear more about this.  Is this one of those things where 80% of the time you don&apos;t care about that level of performance because your hash is relatively small and the convenience of preserving sort order is a better benefit?  Is there some kind of &lt;code&gt;FastHash&lt;/code&gt; class that can be used for cases where you don&apos;t care about ordering and better performance is needed?  Or is the performance of the insertion-order Hash good enough for all cases?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New Regular Expression Engine&lt;/p&gt;

&lt;p&gt;The Oniguruma regular expression engine is used in Ruby 1.9.  This engine supports many advanced regular expression features.  I once looked into directly porting the Perl Markdown code into Ruby but quickly found out that wasn&apos;t possible given the limitations of the Ruby 1.8 regular expression engine, so it&apos;s nice to see Ruby will have a regular expression engine that is on par with that of any other language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object#tap and Symbol#to_proc&lt;/p&gt;

&lt;p&gt;I lumped these together into one because they are both kind of the same thing.  These are two language level features that have become idiomatic Ruby because they have been added in Rails.  The downside has been that if you are not in Rails, you either don&apos;t have these, or you have to define them to use them.  Now that is a problem no more and they are just going to be part of Ruby.  &lt;code&gt;Symbol#to_proc&lt;/code&gt; you should recognize and &lt;code&gt;Object#tap&lt;/code&gt; is the same thing as &lt;code&gt;returning&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
</summary>
    <content type="html">&lt;p&gt;A few days ago, Charles Nutter asserted that &lt;a href=&quot;http://headius.blogspot.com/2008/04/promise-and-peril-for-alternative-ruby.html&quot;&gt;&quot;people are not flocking to Ruby 1.9 in droves&quot;&lt;/a&gt;.  I would consider myself one of those people.  When I thought about Ruby 1.9, I just assumed they were tweaking the syntax a little but mostly working on a VM that would make Ruby faster.  Mainly though, Ruby 1.9 is an unstable, development release, so it&apos;s really just for playing around with it.  The intent is for 1.9 to drive development for Ruby 2.0, and have people flock to Ruby 2.0 once that is ready for prime time.&lt;/p&gt;

&lt;p&gt;Another factor is that up until today, I didn&apos;t really know much about what is planning on being added to Ruby 1.9.  There is &lt;a href=&quot;http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9&quot;&gt;a complete list of changes in Ruby 1.9&lt;/a&gt;, but it wasn&apos;t until I came across &lt;a href=&quot;http://codefluency.com/articles/2008/04/13/migrating-to-ruby-1-9/&quot;&gt;a slideshow by Bruce Williams that covers some of the changes in Ruby 1.9&lt;/a&gt; that I took the time to download, install and try out a few new features.  So I thought I would put together a blog entry of 5 of my favorite changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;New Literal Syntax for Hashes&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{:foo =&amp;gt; 1, :bar =&amp;gt; &quot;two&quot;}&lt;/code&gt; now becomes &lt;code&gt;{foo: 1, bar: &quot;two&quot;}&lt;/code&gt;.  This is probably minor, but I really like it.  It&apos;s a little bit cleaner, although I&apos;m sure &lt;a href=&quot;http://obiefernandez.com/&quot;&gt;Obie&lt;/a&gt; will be sad to see the &lt;a href=&quot;http://www.hashrocket.com/&quot;&gt;hash rocket&lt;/a&gt; go.  Actually, I&apos;m sure the hash rocket will still be supported, it will just become no longer idiomatic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New Proc literal&lt;/p&gt;

&lt;p&gt;But fear not Obie, we lose one operator, the hash rocket, but gain another, the proc rocket.  You can now define a proc like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;double = -&amp;gt;(x) { x * 2 }
puts double.(5) # =&amp;gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That can lead to some pretty crazy things, like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;p = -&amp;gt;(n, name=&quot;it&quot;, &amp;amp;block) {
    n.times{|i| block.call }
    puts &quot;I did #{it} #{n} times&quot;
}


p.(3) do |i| 
  puts &quot;#{i} is #{i.even? ? &apos;not &apos;: &apos;&apos;}odd&quot;
end


p.(3, &quot;foo&quot;) do |i| 
  puts &quot;#{i} is #{i.even? ? &apos;not &apos;: &apos;&apos;}odd&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The difference being that you can now have default values for arguments to a Proc and also you can pass a Block to a Proc.  I could see this being used in metaprogramming.  You could probably do a lot of things with this instead of things that get done using class_eval now.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#Ruby 1.8
class Foo
end
Foo.class_eval &amp;lt;&amp;lt;-CODE
  def bar(x,y=2)
    puts &quot;x =&amp;gt; \#{x}, y =&amp;gt; \#{y}&quot;
  end
CODE      


#Ruby 1.9
class Foo
end
Foo.send(:define_method, :bar, -&amp;gt;(x, y=2) { 
  puts &quot;x =&amp;gt; #{x}, y =&amp;gt; #{y}&quot; 
})
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Not sure yet if that would be better, I guess we&apos;ll see.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hashes preserve insert order&lt;/p&gt;

&lt;p&gt;Say goodbye to using 2D arrays just because you want things to be in a certain order.  This is very convenient, but I was always under the impression that this is the way it was for performance reasons.  I&apos;d like to hear more about this.  Is this one of those things where 80% of the time you don&apos;t care about that level of performance because your hash is relatively small and the convenience of preserving sort order is a better benefit?  Is there some kind of &lt;code&gt;FastHash&lt;/code&gt; class that can be used for cases where you don&apos;t care about ordering and better performance is needed?  Or is the performance of the insertion-order Hash good enough for all cases?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;New Regular Expression Engine&lt;/p&gt;

&lt;p&gt;The Oniguruma regular expression engine is used in Ruby 1.9.  This engine supports many advanced regular expression features.  I once looked into directly porting the Perl Markdown code into Ruby but quickly found out that wasn&apos;t possible given the limitations of the Ruby 1.8 regular expression engine, so it&apos;s nice to see Ruby will have a regular expression engine that is on par with that of any other language.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object#tap and Symbol#to_proc&lt;/p&gt;

&lt;p&gt;I lumped these together into one because they are both kind of the same thing.  These are two language level features that have become idiomatic Ruby because they have been added in Rails.  The downside has been that if you are not in Rails, you either don&apos;t have these, or you have to define them to use them.  Now that is a problem no more and they are just going to be part of Ruby.  &lt;code&gt;Symbol#to_proc&lt;/code&gt; you should recognize and &lt;code&gt;Object#tap&lt;/code&gt; is the same thing as &lt;code&gt;returning&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>Bruce Williams</name>
    </author>
    <id>urn:uuid:d4653043-368a-4ceb-86d4-ec43447911a0</id>
    <published>2008-04-29T11:28:53-04:00</published>
    <updated>2008-04-29T11:28:53-04:00</updated>
    <title type="html">Comment on "What&apos;s to Like in Ruby 1.9" by Bruce Williams</title>
    <link href="http://paulbarry.com/articles/2008/04/29/whats-to-like-in-ruby-1-9#comment-5124" rel="alternate" type="text/html"/>
    <content type="html">Good article; I&apos;m a big fan of 1.9 myself.  I also don&apos;t think the number of people &apos;flocking&apos; makes that big of a difference for an unstable version (I remember 1.7):&lt;br/&gt;&lt;br/&gt;The &apos;hashrocket&apos; use in Hash literals is [at least at this time] still supported, and you can even mix and match, although it&apos;s odd:&lt;br/&gt;  { foo: 1, :bar =&amp;gt; 2, baz: 3 }</content>
  </entry>
  <entry>
    <author>
      <name>Avdi</name>
    </author>
    <id>urn:uuid:c31cbe49-014a-49b9-9618-500732c300ec</id>
    <published>2008-04-29T13:29:40-04:00</published>
    <updated>2008-04-29T13:29:40-04:00</updated>
    <title type="html">Comment on "What&apos;s to Like in Ruby 1.9" by Avdi</title>
    <link href="http://paulbarry.com/articles/2008/04/29/whats-to-like-in-ruby-1-9#comment-5126" rel="alternate" type="text/html"/>
    <content type="html">Unless I am much mistaken, the hash rocket is neither deprecated nor un-idiomatic.  The new syntax is sugar for a very specific kind of hash: the kind where all the keys are Symbols.  Granted, this is a pretty common usage of Hash, especially in Rails.  But hashes are still going to support keys of arbitrary types, for which the hash rocket remains necessary.&lt;br/&gt;&lt;br/&gt;And thank goodness. You don&apos;t appreciate arbitrarily-keyed hashes until you work in a language without them, like Perl (strings only) or Python (immutables only).  This is IMO one of the great unsung design choices of Ruby.</content>
  </entry>
  <entry>
    <author>
      <name>Paul Barry</name>
    </author>
    <id>urn:uuid:f6a0275f-6ca8-4a56-830e-b7336749fc6a</id>
    <published>2008-04-29T15:29:56-04:00</published>
    <updated>2008-04-29T15:29:56-04:00</updated>
    <title type="html">Comment on "What&apos;s to Like in Ruby 1.9" by Paul Barry</title>
    <link href="http://paulbarry.com/articles/2008/04/29/whats-to-like-in-ruby-1-9#comment-5127" rel="alternate" type="text/html"/>
    <content type="html">Ahh....so that&apos;s what you meant by arbitrarily-keyed hashes.  You mean hashes that can have an object of any type as the key.  I agree, that is a nice feature.  Even Java has that, so I guess I&apos;ve been sort of taking that for granted.&lt;br/&gt;&lt;br/&gt;But you are right, the hash rocket will still be needed for hashes with non-symbol keys, but also as you said, that won&apos;t be nearly as common.  So it will still be supported, but won&apos;t be idiomatic for symbol-keyed hashes. </content>
  </entry>
  </feed>