Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby 1.9

2,894 views

Published on

The Ruby 1.9 presentation I gave before the ATL RUG.

Published in: Technology, Education

Ruby 1.9

  1. 1. += 0.1 require ‘yarv’ require ‘syntax-1.9’ require ‘stdlib-1.9’ require ‘unicode’ Stephen Touset <stephen@touset.org>
  2. 2. Play Along at Home $ svn export http://svn.ruby-lang.org/repos/ruby/tags/v1_9_9_0 ruby-1.9.0 $ cd ruby-1.9.0 $ ./configure --program-suffix 1.9 $ make $ make test $ chmod a+x bin/irb $ PATH=. RUBYLIB=quot;./libquot; bin/irb
  3. 3. YARV FTW • Faster • Less Memory • Native threads* • Fun Acronym *concurrency of one
  4. 4. ruby 1.8 ruby 1.9 improvement app_answer 0.460 0.146 68% app_erb 1.655 1.646 0.5% app_factorial 0.762 0.858 -13% app_fib 5.572 1.530 73% app_mandelbrot 2.034 0.848 58% app_pentomino 92.811 50.778 45% app_raise 1.984 2.474 -25% app_strcat 1.414 1.113 21% app_tak 8.306 2.083 75% app_tarai 6.530 1.660 75% app_uri 3.555 2.027 43%
  5. 5. ≈38% faster (62% as slow) ((twice-ish as fast))
  6. 6. Yet Another Meaningless Benchmark (http://www.timestretch.com/FractalBenchmark.html)
  7. 7. The Image Below is Not Photoshopped
  8. 8. ...thus ending the language wars once and for all
  9. 9. Shiny New Syntax • Lambda shortcut • Implicit #call • Literal hashes • Block parameters • Function parameter insanity • Block local variables
  10. 10. lambda, v1.9 ->
  11. 11. λ
  12. 12. _ /|
  13. 13. _ /|
  14. 14. ->
  15. 15. λ -> “I found lambda!” - Matz
  16. 16. c = lambda {|a, b| [b, a] } - becomes - c = ->(a, b) { [b, a] }
  17. 17. Implicit #call c = lambda {|a, b| [b, a] } c.call(1, 2) - becomes - c = ->(a, b) { [b, a] } c.(1,2)
  18. 18. Block Parameters lambda {|a, b, c = 1, &d| d.call } ^ syntax error, unexpected '}' ... - becomes - ->(a, b, c = 1, &d) { d.call }
  19. 19. laaaaame lambda { yield }.call { puts ‘hi’ } LocalJumpError: no block given - becomes - -> { yield }.() { puts ‘hi’ } LocalJumpError: no block given
  20. 20. Literal Hashes { :a => 1, :b => 2, :c => 3, :d => 4 } - becomes - { a: 1, b: 2, c: 3, d: 4 }
  21. 21. W...T... def foo(a, b = 100, c) puts “#{a} #{b} #{c}” end foo(3) # => ArgumentError foo(3, 5) # => 3, 100, 5 foo(3, 4, 5) # => 3, 4, 5
  22. 22. ...F def bar(a, b = nil, *c, d) end
  23. 23. Block Local Variables i = Elephant.new [1,2,3].map {|i| i * 2 } i # => #<Elephant:0x3688b4>
  24. 24. stdlib.succ! • send no longer calls private methods • #methods and its siblings return symbols • class variables are not inherited, now work like class instance variables (thus obsoleting my entire metaprogramming presentation) • blocks passable to #[] • enumerator love • Oniguruma regexp engine
  25. 25. #tap that class Passes an object to a block, then returns the object (useful for call chaining) ‘foo’.tap(&:upcase!).tap {|s| s.gsub! /oo/, ‘u’ }
  26. 26. Wait, did I see that correctly? tap(&:upcase!) ^^
  27. 27. Enumerator Love [1, 2, 3].cycle {|i| puts i } # loops forever puts-ing 1,2,3 [1, 2, 3].first(2) # => [1,2] [1, 2, 3].map.with_index {|o,i| i} # => [0, 1, 2] [1, 3, 4, 2].take {|i| i < 4 } # => [1, 3] [1, 2, 3].reduce(:+1) # => 6
  28. 28. Ordered Hashes h = { a: 1, b: 2, c: 3 } h # => { a: 1, b: 2, c: 3 } h.keys # => [ :a, :b, :c ] h.values # => [ 1, 2, 3 ]
  29. 29. Encoded Strings • No longer Enumerable • String#bytes • String#lines • String#each_char • “One-char-wide” String behavior
  30. 30. #!/usr/bin/ruby -w # -*- coding: utf-8 -*- open(filename, ‘r:utf-8’) encodings = %w{ utf-8 utf-16 ascii-8bit euc-jp }
  31. 31. More Info • Summary of Changes in 1.9 http://eigenclass.org/hiki/Changes+in+Ruby+1.9 • Mechanically Verified 1.9 Changelog http://eigenclass.org/hiki/mechanically-verified-ruby19-changelog • Matz’ RubyConf 2007 Keynote http://rubyconf2007.confreaks.com/d2t1p8_keynote.html
  32. 32. exit(0)

×