Jim Lindley Notes

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"

Sorry, comments are closed for this article.