Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's new in ruby Since 2.0.0

What's new in ruby Since 2.0.0

A short overview of new and changed things in Ruby since 2.0.0 given at the October 2014 Ruby Ireland meetup.

Source: https://github.com/chrismcg/presentations/blob/master/whats_new_in_ruby_since_200/whats_new_in_ruby_since_200.md

Best viewed with Deckset App

Chris McGrath

October 22, 2014
Tweet

More Decks by Chris McGrath

Other Decks in Technology

Transcript

  1. Refinements module Stats refine Array do def mean inject(0) {

    |sum, x| sum + x } / size end end end 12
  2. Refinements a1 = [1, 2, 3, 4] a1.mean rescue "no

    mean method" # => "no mean method" using Stats a2 = [2, 4, 6, 8] a2.mean # => 5 a1.mean # => 2 13
  3. Refinements class Analyzer using Stats def initialize(array) @array = array

    end def summary { mean: @array.mean } end end 14
  4. Refinements a1 = [1, 2, 3, 4] analyzer = Analyzer.new(a1)

    a1.mean rescue "no mean method" # => "no mean method" analyzer.summary # => {:mean=>2} 15
  5. Refinements class Analyzer def short_summary { m: @array.mean } end

    end a1 = [1, 2, 3, 4] analyzer = Analyzer.new(a1) analyzer.summary # => {:mean=>2} analyzer.short_summary rescue "no mean method" # => "no mean method" 17
  6. Need$some$defaults def render(content, opts = {}) width = opts[:width] ||

    1920 height = opts[:height] || 1080 ... end render "* foo" 21
  7. :full%means%use%width%or%height%of%page def render(content, opts = {}) width = opts.fetch(:width) {

    1920 } width = @page.width if width == :full height = opts.fetch(:height) { 1080 } height = @page.height if height == :full ... end render "* foo", :width => :full render "* bar", :hieght => 786 # doesn't pick up on typo 22
  8. With%Ruby%2.x%keyword%arguments def render(content, width: 1920, height: 1080) width = @page.width

    if width == :full height = @page.height if height == :full ... end render "* foo" render "* bar", hieght: 786 # => # ~> -:7:in `<main>': unknown keyword: hieght (ArgumentError) 23
  9. If#the#user#must#specify#width def render(content, width:, height: 1080) width = @page.width if

    width == :full height = @page.height if height == :full end render "* foo" # ~> -:6:in `<main>': missing keyword: width (ArgumentError) 24
  10. Module#include+/+prepend class Analyzer include Stats def mean puts "Analyzer#mean" super

    end end Analyzer.ancestors # => [Analyzer, Stats, Object, Kernel, BasicObject] Analyzer.new.mean # => nil # >> Analyzer#mean # >> Stats#mean # >> Stats#mean no superclass mean method 29
  11. Module#include+/+prepend class Analyzer2 prepend Stats def mean puts "Analyzer2#mean" super

    rescue puts "Analyzer2#mean no superclass mean method" end end Analyzer2.ancestors # => [Stats, Analyzer2, Object, Kernel, BasicObject] Analyzer2.new.mean # => nil # >> Stats#mean # >> Analyzer2#mean # >> Analyzer2#mean no superclass mean method 30
  12. Cache&decorator a = Analyzer.new a.expensive_method # => 42 # >>

    thinking really hard! a.expensive_method # => 42 a.expensive_method # => 42 (This&could&of&course&be&generalized&with&meta&programming) 33
  13. %i#and#%I Create&arrays&of&symbols [1] pry(main)> n = 10 => 10 [2]

    pry(main)> %i(foo bar#{n}) => [:foo, :"bar\#{n}"] [3] pry(main)> %I(foo bar#{n}) => [:foo, :bar10] 37
  14. Excep&on#cause-(2.1) class Downloader class DownloadException < StandardError; end def download!

    begin raise "boom!" rescue RuntimeError raise DownloadException end end end 40
  15. Lazy%enumerators #lazy!can!be!called!on!any!enumerable.!Can!do!FP!style!stuff!like: [25] pry(main)> [1, 2, 3, 4].lazy.cycle.take(20) => #<Enumerator::Lazy:

    ...> [26] pry(main)> [1, 2, 3, 4].lazy.cycle.take(20).to_a => [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4] 42
  16. Kernel#itself+(2.2) [1, 2, 3, 4, 1, 5, 2, 3, 5,

    6].group_by(&:itself) => {1=>[1, 1], 2=>[2, 2], 3=>[3, 3], 4=>[4], 5=>[5, 5], 6=>[6]} (This&is&my&favourite&new&method&name) 45