シュッとふりかえる Ruby 2.0 以降

シュッとふりかえる Ruby 2.0 以降

Ruby 2.0 以降の変更を駆け足でふりかえります。
5分では足りませんでした。

Fb69c72d5af289d6674d2659246a5291?s=128

Tomohiro Nishimura

February 20, 2015
Tweet

Transcript

  1. γϡοͱ;Γ͔͑Δ 3VCZҎ߱ ୈճ3VCZؔ੢ษڧձ ੢ଜ༑༟BLB4JYFJHIU

  2. 4JYFJHIU !UPNPIJ@SP ۙگɿ1FSM

  3. ͬ͘͟Γͱมߋ఺Λ ;Γ͔͑Γ·͢ɻ

  4. ͨͩ͠ɺཧղग़དྷͨͱ͜Ζ΍ɺ ڵຯͰϑΟϧλ͍ͯ͠·͢ɻ

  5. ೥݄̎೔ 3VCZੜ஀प೥

  6. 3VCZ

  7. ΩʔϫʔυҾ਺ ! def meth(foo: “bar”, hoge: “piyo”) [foo, hoge] end

    ! p meth(foo: “cat”) #=> [“cat”, “piyo”]
  8. 3FpOFNFOUT w ޡղΛڪΕͣʹ͍͏ͱ ಛఆͷείʔϓͷதͰͷΈ ༗ޮͳϝιουΛఆٛ͢Δํ๏ w &YQFSJNFOUBMͳػೳͱͯ͠ೖΓ·ͨ͠

  9. .PEVMFQSFQFOE module Awesome def greet "awesome " + super end

    end ! class Person prepend Awesome def greet "hi!" end end ! puts Person.new.greet #=> awesome hi! w ܧঝͷઌ಄ʹૠ͠ࠐΊΔػೳ ! w JODMVEF"XFTPNF w 1FSTPO"XFTPNF w QSFQFOE"XFTPNF w "XFTPNF1FSTPO ! w 3*1BMJBT@NFUIPE@DIBJO
  10. σϑΥϧτΤϯίʔσΟϯά ͕65'ʹͳͬͨ w FODPEJOHVUG͕ෆཁʹͳͬͨʂʂʂʂ

  11. &OVNFSBCMFMB[Z users = [ 5ԯ݅ͷσʔλ ] p users.each.lazy. map {|u|

    u.newbie? }. map {|u| u.rubyist? }. map {|u| u.age < 20 }. to_a
  12. ͦͷଞ w J w @@EJS@@ w )BTI OJMUP@I )BTIUP@I w

    DPOTU@HFU l'PP#BS#B[z  w "SSBZCTFBSDI w 5SBDF1PJOU w HFNŠOPEPDVNFOU
  13. ೥݄೔

  14. 3VCZ

  15. 3(FO($ʹͳͬͨ w ύϑΥʔϚϯε޲্ʂ

  16. ΩʔϫʔυҾ਺ͷ֦ு ! def meth(foo:, hoge:) [foo, hoge] end ! p

    meth(foo: “cat”) #=> `meth': missing keywords: hoge (ArgumentError)
  17. ϝιουఆ͕ٛ γϯϘϧΛฦ͢Α͏ʹ class A private def method # ... end

    end ͭ·ΓEFGIPHFFOEIPHF
  18. #JOEJOHMPDBM@WBSJBCMF@ HFUcTFU def span(body, class: "") html_class = binding.local_variable_get(:class) %q!<span

    class="%s">%s</span>! % [html_class, body] end ! p span("hoge", class: “right”) ! #=> "<span class=\"right\">hoge</span>" ϩʔΧϧม਺Λऔಘͨ͠ΓઃఆͰ͖Δ ༧໿ޠ΋ΩʔϫʔυҾ਺ͱͯ͠࢖͑Δ
  19. 3FpOFNFOUT module Bangable refine String do def ! self +

    "!" end end end ! class Person using Bangable ! def greet(name) puts "Hi #{name}".! end end ! Person.new.greet("cat") #=> Hi cat! ! puts "Hi dog".! #=> false w &YQFSJNFOUBM͕औΕͨʂ ! w ΫϥεϞδϡʔϧͷε ίʔϓʹ΋༗ޮʹ w FWBM଒ͷରԠ͸ೖΒͳ ͔ͬͨ
  20. .PEVMFJODMVEFQSFQFOE͕ QVCMJDͳϝιουʹͳͬͨ A.include AwesomeModule A.prepend AwesomeModule A.__send__(:include, AwesomeModule) A.__send__(:prepend, AwesomeModule)

  21. GSFF[F w શͯͷ4ZNCPM͕ϑϦʔζ͞ΕΔΑ͏ʹͳͬͨ w ϑϦʔζ͞Εͨจࣈྻ͕ಉ͡PCKFDU@JEΛฦ͢Α ͏ʹͳͬͨ w ϋογϡͷΩʔʹจࣈྻΛࢦఆ͢ΔͱϑϦʔζ͞Ε ΔΑ͏ʹͳͬͨ4ZNCPMͱಉ͡ѻ͍

  22. ͦͷଞ w "SSBZUP@I w S J SJ w &YDFQUJPODBVTF w

    ,FSOFMTJOHMFUPO@NFUIPE ,FSOFMTJOHMFUPO@NFUIPE  w 4USJOHTDSVC w FWBM଒͕؀ڥΛҾ͖ܧ͕ͳ͘ͳͬͨ w MBNCEB1SPD͔ΒͷSFUVSOͷڍಈͷ౷Ұ w BOENPSF
  23. ೥݄೔

  24. 3VCZ

  25. \lTZNCPMLFZzlWBMVFz^ {:"symbol-key" => "value"} {“symbol-key": "value"}

  26. ($ w ΠϯΫϦϝϯλϧ($ͷಋೖ w γϯϘϧ͕($ͷର৅ʹͳͬͨ

  27. #JOEJOH w #JOEJOHMPDBM@WBSJBCMFT w #JOEJOHSFDFJWFS class A def context x

    = 1 y = 2 binding end end ! a = A.new p a.context.local_variables #=> [:x, :y] p a.context.receiver #=> #<A:0x007fa450833dc0>
  28. .FUIPETVQFS@NFUIPE class Parent def meth end end ! class Child

    < Parent def meth end end ! p Child.new.method(:meth).super_method #=> #<Method: Parent#meth> TVQFSͰݺͼग़ͤΔϝιουΛऔಘग़དྷΔΑ͏ʹͳͬͨ
  29. .FUIPEDVSSZ def add(a, b) a + b end ! add_ten

    = method(:add).curry.(10) p add_ten.(5) #=> 15 1SPDDVSSZͱಉ͜͡ͱ͕ग़དྷΔΑ͏ʹͳͬͨ
  30. &OVNFSBCMF w TMJDF@BGUFS w TMJDF@CFGPSFͷٯ w NBY NBY@CZ NJO NJO@CZ

    w Ҿ਺ΛऔΕΔΑ͏ʹͳͬͨ counts = [1, 2, 3, 4, 5] ! counts.slice_before(&:even?) #=> [[1, 2], [3, 4], [5]] ! counts.max(2) #=> 5, 4 counts.min(3) #=> 1, 2, 3
  31. 0CKFDUJUTFMG state = state.presence_in(AVAILABLE_STATES) Book.public_send(state || :itself).order(desc: :price) ࣗ෼ࣗ਎Λฦ͢ϝιου ྫ͑͹͜͏࢖͏

    ࢦఆ͞Εͨεςʔτ͕༗ޮͳ΋ͷͰ͋Ε͹ͦͷத͔Βɺ ແޮͳ΋ͷͰ͋Ε͹શମ͔Βݕࡧ͢Δɻ #PPLTBMFPSEFS EFTDQSJDF ΋͘͠͸#PPLPSEFS EFTDQSJDF
  32. $PNQBSBCMF class Person include Comparable ! attr_reader :age ! def

    initialize(age) @age = age end ! def <=>(other) self.age <=> other.age end end ! a = Person.new(20) b = Person.new(18) ! p a > b #=> true ! p a == nil #=> warning: Comparable#== will no more rescue exceptions of #<=> in the next release. #=> warning: Return nil in #<=> if the comparison is inappropriate or avoid such comparison. #=> false ஫ ࣍ͷόʔδϣϯ͔Βͷྫ֎ΛӅṭͯ͘͠Εͳ͘ͳΓ·͢ɻ
  33. ͦͷଞ w OJMUSVFGBMTF͕GSP[FOʹมߋ w 1BUIOBNF lDVUFz lDBUzlEPHz w 'JMFCJSUIUJNF w

    'MPBU OFYUcQSFW @qPBU w .BUSJYpSTU@NJOPS .BUSJYDPGBDUPS w %JHFTU)."$͕ͳ͘ͳͬͨ 0QFO44-)."$  w $"1*ͷ%FQSFDBUFE͕ফ͑ͯΔ w ࡉ͔͍มߋ͕ͨ͘͞Μ͋Δ
  34. ͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ɻ