Ruby 1.9 Changes in a nutshell

  • 1,505 views
Uploaded 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! …

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.

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,505
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Changes in Ruby 1.9Why switch?
  • 2. So why switch?•1.8.7 is fine!•So is Windows XP and IE6 AMIRITE?• Your grandma still uses 1.8.7
  • 3. 7 BIG CHANGES7 small changes
  • 4. THE BIG CHANGES
  • 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
"Thats
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
"wont
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)
  • 12. the small changes
  • 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!
  • 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. 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. BONUS!
  • 21. Hundreds of new features: $0Check out the PickAxe for more!