Ruby 1.9's been around for years, but there's still a lot of mystery about what's different from 1.8.7. Short answer: a lot!
Presentation from 8/23/11 presentation at Utah Ruby Users Group.
5. 1. Rubygems & Rake included
•No more require ‘rubygems’
6. 2. Fibers
• Work like procs with persistent state
• Required to use EventMachine, Goliath, etc.
fb = Fiber.new do |val|
Fiber.yield "That's all... (#{val})"
Fiber.yield "folks! (#{val})"
end
puts fb.resume 10
...
puts fb.resume 20
(stolen from RubyLearning)
7. 3. Ordered hashes
• You already know this
• @dbrady showed why hash syntax is thoroughly
random in data structures talk
• 1.9 lays a list on top of a hash for ordering
• New JSON-y syntax (but only for symbol keys):
data = { jan: 201234, feb: 234234, mar:
234345 }
8. 4. MiniTest
• Ground-up rewrite of Test::Unit
• Includes MiniTest::Unit & MiniTest::Spec
require 'minitest/spec'
describe Meme do
before do
@meme = Meme.new
end
describe "when asked about cheeseburgers" do
it "should respond positively" do
@meme.i_can_has_cheezburger?.must_equal "OHAI!"
end
end
describe "when asked about blending possibilities" do
it "won't say no" do
@meme.does_it_blend?.wont_match /^no/i
end
end
end
(stolen from www.bootspring.com )
9. 5. BasicObject
• All objects inherit from BasicObject
• Stripped-down Object, ideal for metaprogramming
10. 6. YARV (aka Koichi’s Ruby
• Replacement for MRI
• Faster than MRI for MOST things (but not, apparently
starting a Rails instance)
11. 7. Shiny new RegEx engine
• Named Matches == Much smarter RegEx
• Nest named groups to create subroutines
pattern = /(?<hour>dd):(?<min>dd):(?
<sec>dd)/
string = "It is 12:34:56 precisely"
if match = pattern.match(string)
puts "Hour = #{match[:hour]}"
puts "Min = #{match[:min]}"
puts "Sec = #{match[:sec]}"
end
(stolen from PragPub)
13. 1. Symbols == Strings (in RegEx)
a = [:windows, :mac, :amiga]
puts a.grep(/ac/)
# 1.8 => []
# 1.9 => mac
(stolen from PragPub)
14. 2. String#each is dead
• Long live String#each_line
•Can create issues for cross-compatibility
if RUBY_VERSION < "1.9"
require "enumerator"
class String
def lines
enum_for(:each)
end
end
end
(stolen from @jeg2)
15. 3. Splat anywhere
• Splat argument can live in any argument, not just the
last
def many_args (first, *middle, last)
puts first
middle.each { |arg| puts arg }
puts last
end
16. 4. More definable operators
• You can define operators like!
• Still protected: not, and, or, ||, &&
17. 5. No need for .chr
• Strings are actually collections of characters now
•String#each_character now returns actual characters!
19. 7. Encoding changes
• Actually, kind of a big deal; affects all String
operations
• Per-file encoding possible with a first-line comment:
# encoding: utf‐8
str = "∂og"
puts str.length
puts str[0]
puts str.reverse
(stolen from PragPub)