Ruby 2.0

ALL THE THINGS


Sasha Gerrand
@sgerrand

tl;dr

  • 1.8.7 is officially EOL'd
  • 2.0.0 is (noticeably) faster than 1.9.3
    (GC, Float ops, Kernel#require, VM optimizations)

  • Drop-in replacement
    100% compatible with 1.9.3
  • New language core features
    (keyword args, Module#prepend, %i literal, __dir__, etc)
  • New built-in library features
    (Enumerable#lazy, #to_h, etc)
  • Debug support
    DTrace and TracePoint support

Embrace the awesome


NEW LANGUAGE CORE FEATURES

KEYWORD ARGUMENTS

Current idiom:
def my_method(argument1, argument2, opts = {})
  opts[:value] ||= 1
end
New style:
def my_method(argument1, argument2, value: 1, *a, **kw)
  # assign away
end

  • Array to argument list using splat (*) operator
  • Hash to keyword arguments using double splat (**)

Module#prepend

No need to use alias_method_chain or monkey patching

module A
  def self.prepended(mod)
    puts "#{self} prepended to #{mod}"
  end
end
module Enumerable
  prepend A
end
 # => prints "A prepended to Enumerable"

New array of symbols literals

%i (interpolation disabled)
%i[one one-hundred\ one]
#=> [:one, :"one-hundred one"]

%I (interpolation allowed)
%w[one one-hundred\ #{2 * 10}]
#=> ["one", "one-hundred 20"]

__dir__

  • New Kernel method, __dir__
  • Equivalent to File.dirname(File.realpath(__FILE__))

UTF-8 as default encoding

Finally the default file encoding

①, ②, ③ GO!

New Core Library Features

#to_h (finally)

Available in these classes:
  • Hash (surprise!)
  • NilClass
  • Struct
Customer = Struct.new(:name, :address, :zip)
sasha = Customer.new("Sasha Gerrand", "1 Kent St, Sydney NSW", 2000)
sasha.to_h[:address]   #=> "1 Kent St, Sydney NSW"

#LAZY evaluation

Current idiom:

module Enumerable
  def filter_map(&block)
    map(&block).compact
  end
end

New style:

class Enumerator::Lazy
  def filter_map
    Lazy.new(self) do |yielder, *values|
      result = yield *values
      yielder << result if result
    end
  end
end

(1..Float::INFINITY).lazy.filter_map{|i| i*i if i.even?}.first(5)
    # => [4, 16, 36, 64, 100]

#size of things

Enumerator#size

(1..100).to_a.permutation(4).size # => 94109400
loop.size # => Float::INFINITY
(1..100).drop_while.size # => nil

Range#size

(10..20).size    #=> 11

Onigmo

  • New regular expression engine*
  • Replaces Onigurama (standard in 1.9)
  • Supports new features from Perl 5.10+








AsyncHRONOUS exception handling

Finer control over Thread#raise and Thread#kill

Thread.handle_interrupt
th = Thread.new do
  Thread.handle_interrupt(RuntimeError => :never) {
    begin
      # You can write resource allocation code safely.
      Thread.handle_interrupt(RuntimeError => :immediate) {
        # ...
      }
    ensure
      # You can write resource deallocation code safely.
    end
  }
end
Thread.pass
# ...
th.raise "stop"

Enough rambling

Go forth: install or upgrade!

  • Default Ruby interpreter on OS X 'Mavericks'
  • Other options:
    • chruby
    • rbenv
    • RVM
    • RubyInstaller
    • compile from source

Questions?

Thanks

Happy 20th birthday, Ruby!









Sasha Gerrand
@sgerrand

Ruby 2.0 ALL THE THINGS

By Sasha Gerrand

Ruby 2.0 ALL THE THINGS

A quick overview of why everyone should upgrade their Rubies from 1.9 to 2.0.

  • 1,726