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

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

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

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

Tomohiro Nishimura

February 20, 2015
Tweet

More Decks by Tomohiro Nishimura

Other Decks in Programming

Transcript

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

    ! p meth(foo: “cat”) #=> [“cat”, “piyo”]
  2. .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
  3. &OVNFSBCMFMB[Z users = [ 5ԯ݅ͷσʔλ ] p users.each.lazy. map {|u|

    u.newbie? }. map {|u| u.rubyist? }. map {|u| u.age < 20 }. to_a
  4. ͦͷଞ 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
  5. ΩʔϫʔυҾ਺ͷ֦ு ! def meth(foo:, hoge:) [foo, hoge] end ! p

    meth(foo: “cat”) #=> `meth': missing keywords: hoge (ArgumentError)
  6. #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>" ϩʔΧϧม਺Λऔಘͨ͠ΓઃఆͰ͖Δ ༧໿ޠ΋ΩʔϫʔυҾ਺ͱͯ͠࢖͑Δ
  7. 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଒ͷରԠ͸ೖΒͳ ͔ͬͨ
  8. ͦͷଞ w "SSBZUP@I w S J SJ w &YDFQUJPODBVTF w

    ,FSOFMTJOHMFUPO@NFUIPE ,FSOFMTJOHMFUPO@NFUIPE  w 4USJOHTDSVC w FWBM଒͕؀ڥΛҾ͖ܧ͕ͳ͘ͳͬͨ w MBNCEB1SPD͔ΒͷSFUVSOͷڍಈͷ౷Ұ w BOENPSF
  9. #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>
  10. .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Ͱݺͼग़ͤΔϝιουΛऔಘग़དྷΔΑ͏ʹͳͬͨ
  11. .FUIPEDVSSZ def add(a, b) a + b end ! add_ten

    = method(:add).curry.(10) p add_ten.(5) #=> 15 1SPDDVSSZͱಉ͜͡ͱ͕ग़དྷΔΑ͏ʹͳͬͨ
  12. &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
  13. 0CKFDUJUTFMG state = state.presence_in(AVAILABLE_STATES) Book.public_send(state || :itself).order(desc: :price) ࣗ෼ࣗ਎Λฦ͢ϝιου ྫ͑͹͜͏࢖͏

    ࢦఆ͞Εͨεςʔτ͕༗ޮͳ΋ͷͰ͋Ε͹ͦͷத͔Βɺ ແޮͳ΋ͷͰ͋Ε͹શମ͔Βݕࡧ͢Δɻ #PPLTBMFPSEFS EFTDQSJDF ΋͘͠͸#PPLPSEFS EFTDQSJDF
  14. $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 ஫ ࣍ͷόʔδϣϯ͔Βͷྫ֎ΛӅṭͯ͘͠Εͳ͘ͳΓ·͢ɻ
  15. ͦͷଞ 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 ࡉ͔͍มߋ͕ͨ͘͞Μ͋Δ