Ruby 1.9 Changes in a nutshell

1,812 views

Published on

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.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Ruby 1.9 Changes in a nutshell

  1. 1. Changes in Ruby 1.9Why switch?
  2. 2. So why switch?•1.8.7 is fine!•So is Windows XP and IE6 AMIRITE?• Your grandma still uses 1.8.7
  3. 3. 7 BIG CHANGES7 small changes
  4. 4. THE BIG CHANGES
  5. 5. 1. Rubygems & Rake included•No more require
‘rubygems’
  6. 6. 2. Fibers• Work like procs with persistent state• Required to use EventMachine, Goliath, etc. fb
=
Fiber.new
do
|val| 

Fiber.yield
"Thats
all...
(#{val})" 

Fiber.yield
"folks!
(#{val})" end puts
fb.resume
10 ... puts
fb.resume
20(stolen from RubyLearning)
  7. 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. 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
"wont
say
no"
do 





@meme.does_it_blend?.wont_match
/^no/i 



end 

end end(stolen from www.bootspring.com )
  9. 9. 5. BasicObject• All objects inherit from BasicObject• Stripped-down Object, ideal for metaprogramming
  10. 10. 6. YARV (aka Koichi’s Ruby• Replacement for MRI• Faster than MRI for MOST things (but not, apparently starting a Rails instance)
  11. 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)
  12. 12. the small changes
  13. 13. 1. Symbols == Strings (in RegEx) a
=
[:windows,
:mac,
:amiga] puts
a.grep(/ac/) #
1.8
=>
[] #
1.9
=>
mac (stolen from PragPub)
  14. 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. 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. 16. 4. More definable operators• You can define operators like!• Still protected: not,
and,
or,
||,
&&
  17. 17. 5. No need for .chr• Strings are actually collections of characters now•String#each_character now returns actual characters!
  18. 18. 6. Proc changes• Symbol#to_proc now built-in & optimized thingy
{|i|
i.methody_thing
} thingy
(&:methody_thing)• #proc != lambda, #proc == Proc.new
  19. 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)
  20. 20. BONUS!
  21. 21. Hundreds of new features: $0Check out the PickAxe for more!

×