Jim Lindley Notes

MacRuby

February 28th, 2008

Apple has just released a development version of MacRuby, Ruby 1.9 built on top of Objective-C. This is very interesting, and is only the second ever Apple language that will have Cocoa support built in from the ground up.

Also, it includes keyword argument support, something missing from current Ruby implementations. It will be interesting to see if this is the first alternate Ruby implementation that will truly fork from the C-based Ruby language. I really hope that it doesn’t end up becoming so.

I’d recommend modifying the Apple installation instructions (adding a directory in your home folder, and then just editing the configure line), like so:

mkdir -p ~/macruby
./configure --enable-shared \
            --prefix=/Users/[username]/macruby \
            --program-prefix=mac

That way ‘ruby’ still points to the current production quality Ruby 1.8.6 installed with Leopard, no gem folders are stepped on, and you can access MacRuby via ‘macruby’, ‘macirb’, etc, if you add the macruby’s bin folder to your path.

January: Month of Ruby 1.9

January 10th, 2008

[UPDATE: I’ve been terribly sick the last week so this series is delayed a bit, sorry!]

In January I’ll be running a series of posts – one each weekday – highlighting a new feature or change in Ruby 1.9, which had a developer release on Christmas Day. While Ruby 1.9 should not yet be used in production, it is now the perfect time to install it alongside 1.8 and get your hands dirty.


Getting Ruby 1.9


On a Linux/Unix/Mac installation should be straightforward if you are used to compiling packages.

Download:

mkdir ~/src
cd ~/src  
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
# or curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
tar xzvf ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz

Install:

cd ruby1.9.0-0
autoconf
./configure --prefix=/usr/local --program-suffix=19
make
sudo make install

Now in addition to ruby, irb, and ri you should have available to you ruby19, irb19, and ri19.

Start up irb19 and check the ruby version:

jlindley$ irb19
>> RUBY_VERSION
=> "1.9.0"

Don’t expect to be able to run Rails and all your usual programs as normal, there is still a lot of work going on updating Rails and other gems to be 1.9 compatible. This release breaks some old things, but going forward 2.0 and further should all be compatible with 1.9. This is the painful release.

So, install 1.9 and check back on January 1st for the first entry in this series. In the meantime, a summary of changes in 1.9 can (sometimes) be found at Eigenclass.org.


Series Index


This section will be updated with links as the month progresses, but an outline of topics is:

Week 1

[UPDATE: I’ve been terribly sick the last week so this series is delayed a bit, sorry!]

Ruby 1.9: Gems Built In

January 2nd, 2008

If you’ve got Ruby 1.9 installed, you’ve also got the RubyGems package management system installed. It’s been moved into the core distribution, with an eye towards repackaging the standard libraries as gems (but still included by default) to allow for a more manageable development process.

Gems aren’t loaded and don’t take up memory until you require them. You no longer need to require gem itself before asking to load a gem.

>> VERSION
=> "1.8.6"
>> require 'rake'
  LoadError: no such file to load -- rake
>> require 'rubygems'
=> true
>> require 'rake'
=> true

But now with 1.9:

>> RUBY_VERSION
=> "1.9.0"
>> require 'rake'
=> true

Most gems are not updated yet for 1.9 compatibility, check before using any. Gems now are supposed to include a Ruby compatibility identifier. If you come across a gem without one it’s pretty safe to say that it won’t work on 1.9.

This list is not exhaustive, but does cover the issues I’ve had in a first pass at porting my code to 1.9. For instructions on installing Ruby 1.9, please see the introduction to this series of posts. As a reminder, do not use 1.9 for production systems yet.

Ennumerable#zip

Ruby 1.8 allowed padding of zipped lists with nil, and returned the result as a new array:

>> VERSION
=> "1.8.6"
>> [1,2,3].zip(['a','b'])
=> [[1, "a"], [2, "b"], [3, nil]]

In 1.9 zip returns an enumberable object and does not pad short collections. It will blow up once you run out of matching items:

>> RUBY_VERSION
=> "1.9.0"
>> [1,2,3].zip(['a','b'])
=> #<Enumerable::Enumerator:0x3b48e0>
>> e = [1,2,3].zip(['a','b'])
=> #<Enumerable::Enumerator:0x3b01c8>
>> e.next
=> [1, "a"]
>> e.next
=> [2, "b"]
>> e.next
StopIteration: iteration reached at end
    from (irb):5:in `next'

Use Hash#key instead of Hash#index:

>> my_list = {:foo => "bar"}
=> {:foo=>"bar"}
>> my_list.index("bar")
(irb):18: warning: Hash#index is deprecated; use Hash#key
=> :foo
>> my_list.key("bar")
=> :foo

File.exists?(filename) is dead. Use File.exist?(filename) – singular – instead.

Base64 is gone.

>> require "base64"
LoadError: no such file to load -- base64
    from (irb):16:in `require'

No keyed array literals using commas:

>> VERSION
=> "1.8.6"
>> {1,2}
=> {1=>2}

>> RUBY_VERSION
=> "1.9.0"
>> {1,2}
SyntaxError: (irb):10: syntax error, unexpected ',', expecting tASSOC

String#[] returns a character, not a character number.

>> VERSION
=> "1.8.6"
>> "abcde"[0]
=> 97

>> RUBY_VERSION
=> "1.9.0"
>> "abcde"[0]
=> "a"

And finally, VERSION is removed in favor of RUBY_VERSION.

>> VERSION
NameError: uninitialized constant VERSION
  from (irb):1
  ...
  from /usr/local/bin/irb19:13:in `<main>'
>> RUBY_VERSION
=> "1.9.0"

WNY Ruby User Group

December 29th, 2007

If you are in Upstate NY, consider checking out the WNY Ruby User Group. Our next meeting is going to be held on January 7th.

Ruby Rags

December 17th, 2007

Ruby Rags, themed t-shirts. I bought this one:

Enterprise Ready

RubyConf 2007 Videos Up

December 15th, 2007

The videos of the Ruby Conf 2007 sessions are up, come of the best to check out are:

Unfortunately, only part of the most memorable session at Ruby Conf 2007 - Hurting Code for Fun and Profit by Ryan Davis - was recorded.

Rails 2.0 Released

December 7th, 2007

Just saw it was tagged in the Rails dev subversion repository. Long time coming but definitely worth it, I’ve been running on edge for 6 months now and Rails 1.2 is hard to go back to when I need to work on older projects.

Official announcement and an overview of changes.

Rails 2.0 Installation


The actual release version is 2.0.1, 2.0.0 had a small issue. EDIT 12/16: the latest version is now 2.0.2. To install it as a gem:

sudo gem install rails

Or inside an existing Rails project:

rake rails:freeze:edge TAG=rel_2-0-2
rake rails:update


Online Resources:


Books with Rails 2 content:


RubyConf 2007

November 14th, 2007

Matz (Keynote)

It was a great time! Looking forward to next year already.

Merb 0.4.0

November 7th, 2007

If you’re looking for a lighter-weight web development framework then Rails, this is Merb is the place to be.

Merb has just released 0.4.0, a release which came about after much love at RubyConf last week. There is now an official Merb site, Merbivore. Check out the list of new features.

AFK: Headed to Ruby Conf

October 31st, 2007

I’ll be in Charlotte, NC for Ruby Conf through early next week. Will resume posting once I’ve returned.