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

What's Cooking In Ruby 2.7

74f896b312b786ee75a18073941e2457?s=47 Akinori MUSHA
February 27, 2019

What's Cooking In Ruby 2.7

74f896b312b786ee75a18073941e2457?s=128

Akinori MUSHA

February 27, 2019
Tweet

Transcript

  1. 8IBUT$PPLJOH*O 3VCZ ෢ऀথل !LOV

  2. ࣗݾ঺հ w ෢ऀথل"LJOPSJ.VTIB w 5XJUUFSIUUQTUXJUUFSDPNLOV w (JU)VCIUUQTHJUIVCDPNLOV w גࣜձࣾϚνϚν w

    3VCZίϛολʔ w 'SFF#4%ίϛολʔ w 5FBN/PLPHJSJ w )VHJOOڞಉ։ൃऀ
  3. ࣗݾ঺հ w ࢓ࣄʢൈਮʣ w γʔαʔϒϩά͓Αͼ0&.αʔϏε w ΠʔϓϥεެࣜΞϓϦͷόοΫΤϯυ w ૬੮԰ެࣜΞϓϦͷόοΫΤϯυ w

    ۙ͝ॴ4/4ϚνϚν
  4. ϚνϚν w גࣜձࣾϚνϚν w ໨ࠇ۠ɺֶܳେֶӺ͔Βెา෼ w ձࣾ঺հIUUQCJUMZNBDIJNBDIJJOUSP w ʮͻΒ͔Εͨɺͭͳ͕Γͷ͋Δ஍ҬࣾձΛͭ͘Δʯ w

    ۙ͝ॴ4/4ˍ஍Ҭ৘ใϝσΟΞIUUQTNBDIJNBDIJDPN w ΤϯδχΞ࠾༻தʂIUUQTXXXXBOUFEMZDPNDPNQBOJFTNBDIJNBDIJJODQSPKFDUT w ϚνϚνΛࢧ͑Δٕज़$IUUQTUDP0.FIZ-:& 3BJMT 3FBDU 1PTUHSF42- ʜ  w ٕज़ϒϩάIUUQTUFDINBDIJNBDIJDPN
  5. 3VCZͰͷ։ൃ׆ಈ w ϨϙδτϦ؅ཧऀ w ։ൃΠϯϑϥ੔උ w ඪ४ϥΠϒϥϦͷॆ࣮ w %JHFTUͷ֦ுɺϋογϡΞϧΰϦζϜ௥Ճ w

    4FUͷ࣮૷ w CJHEFDJNBM TZTMPH JQBEES [MJCͳͲͷΠϯϙʔτࢧԉ΍อक w 4IFMMXPSETʹTIFMMFTDBQF TIFMMKPJOΛ࣮૷ w 3VCZͷϦϦʔε
  6. 3VCZͰͷ։ൃ׆ಈ w ૊ࠐΫϥεͷ֦ு w "SSBZTMJDF w &OVNFSBUPSͷಋೖ &OVNFSBCMFͷ֦ு w FBDI@DPOT

    FBDI@TMJDF w FBDI@XJUI@PCKFDU
  7. 3VCZͰͷ։ൃ׆ಈ w ࠷ۙ͸ɺ݄࣍ͷ3VCZ։ൃऀձٞʹग़੮ͯ͠ɺओʹ3BJMT 8FC։ൃऀͷ؍఺͔ΒΠϯϓοτͨ͠Γɺࣗ෼͕୲౰͢ ΔϥΠϒϥϦΛ͍͡Δ׆ಈ͕ϝΠϯͰ͢ɻ

  8. 3VCZʹ޲͚ͯ w ·͕ͩग़͔ͯΒϲ݄͔͠ܦ͍ͬͯ·ͤΜ͕ɺ΍ʹ޲͚ͨػೳ։ൃ΍ٞ ࿦͸͍Ζ͍Ζਐߦ͍ͯ͠·͢ɻͦͷҰ୺Ͱ΋ɺָ͘͠͝঺հͰ͖ͨΒͱࢥ͍·͢ɻ w ͜͜Ͱ͝঺հ͢Δͷ͸͋͘·ͰዞҙతʹબΜͩྫʹա͗ͣɺ໢ཏੑ͸͋Γ·ͤΜɻ w ڵຯͷ͋Δํ͸/&84ϑΝΠϧɺ3VCZ։ൃऀձٞͷٞࣄ࿥ɺ3FENJOFͰͷν έοτϕʔεͷٞ࿦Λ௥͍͔͚·͠ΐ͏ɻ w

    ։ൃऀಉ࢜ͷٞ࿦͸ඇৗʹଟ૬తͰߴจ຺Ͱ͕͢ɺʮ͍ΖΜͳ͜ͱ͕͋ΔΜͩͳ͋ʯ ఔ౓ͷड͚औΓͰߏ͍·ͤΜɻ w ։ൃਞ΁ͷϑΟʔυόοΫ͸ɺ͍ΖΜͳνϟϯωϧΛ௨ͯ͡ؾܰʹͲ͏ͧʂ5XJUUFS ͰͭͿ΍͚ͩ͘Ͱ΋໨ʹೖΔ͔΋
  9. ͓͠ͳ͕͖ w ೖͬͨ΋ͷ w ৽จ๏obj.:meth w ແϒϩοΫQSPDͷஈ֊తഇࢭ w &OVNFSBCMFUBMMZ w

    ٫Լ͞Εͨ΋ͷ w ։ൃதɾݕ౼தͷػೳ w ࠓޙͷ3VCZͷ༧ఆ
  10. obj.:meth w obj.method(:meth)ͷ୹ॖจ๏ w ϝιουΦϒδΣΫτΛऔΓग़͢ w ैདྷͷ0CKFDUNFUIPE͸ϝιουݺͼग़ͩͬͨ͠ w ϝιουΛऔΔͨΊʹϝιουΛݺͿඞཁ͕͋Δʜ
 ͍͔ʹ΋3VCZΒ͍͚͠Ͳʜ

    w ΦʔόʔϥΠυ͞Ε͍ͯΔ͔΋͠Εͳ͍
 ʢྫ3FRVFTUΦϒδΣΫτ)551ϝιουΛฦ͢ʣ w 0CKFDUͰͳ͘#BTJD0CKFDUΛܧঝͨ͠ΦϒδΣΫτʹ͸NFUIPEϝιο υ͕ͳ͍
  11. obj.:meth w ೚ҙͷΦϒδΣΫτʹରͯ͠ϝιουΛऔΓग़͍ͨ͠ͱ͖͸ɺΫϥε ͔ΒϝιουΛऔΓग़ͯ͠CJOEDBMM͢Δͱ͍͏ᷖԕͳखஈΛऔΔ ඞཁ͕͋ͬͨ • Object.instance_method(:method)
 .bind(obj)
 .call •

    ΦϒδΣΫτͷૉੑΛ໰ΘͣɺϝιουΛऔΓग़͢୹͍ه๏͕΄͍͠ • ͍͔ͭ͘Ҋ͕͕͋ͬͨɺطଘจ๏ͱͷিಥ͕ͳ͘ɺγϯϘϧه๏ ͔ΒϝιουΛ࿈૝͠΍͍͢ʮobj.:methʯʹܾఆ
  12. obj.:meth w ͨͱ͑͹ɺ͜͏͍͏ίʔυ͕୹͘ॻ͚ΔΑ͏ʹ w lines.map { |line| JSON.parse(line) }
 ↓


    lines.map(&JSON.:parse) • ͨͩ͠ɺself͸লུͰ͖ͳ͍͜ͱͱͨ͠ w lines.each { |line| puts line }
 ↓
 lines.each(&self.:puts) w obj.each(&.:meth)Λڐͯ͠͠·͏ͱɺobj.each(&:meth)ͱݟͨ໨͕ ӝೋͭͳͷʹશવҧ͏ҙຯʹͳΔͷͰ໨ʹ༏͘͠ͳ͍ɺͱ͍͏.BU[ͷࢦఠʹ ΑΔ
  13. ແϒϩοΫQSPDͷஈ֊తഇࢭ w ϝιουఆٛதͰϒϩοΫͳ͠ͷQSPDʹΑͬͯϝιουʹ౉͖ͬͯͨϒϩοΫΛ औಘ͢Δ͜ͱ͕Ͱ͖Δ • def each_key
 keys.each(&proc)
 end •

    proc͸Proc.newͱॻ͍ͯ΋ಉ͡ɺଞʹlambda΋͋Δ • block_given?ͰϒϩοΫ͕౉͞Ε͔ͨͲ͏͔Λ൑ఆͰ͖ɺyieldͰݺͼग़ͤ Δͷͱ߹Θͤͯɺ໌ࣔతʹҾ਺Λॻ͔ͳͯ͘΋౉͞ΕͨϒϩοΫΛར༻Ͱ͖ͨ w ͜ͷػೳΛഇࢭ͠Α͏ͱ͍͏ํ޲ʹͳͬͨ w ແϒϩοΫprocͱProc.new͸͔ΒܯࠂɺແϒϩοΫlambda͸ഇࢭ
  14. ແϒϩοΫQSPDͷஈ֊తഇࢭ w എܠͱͯ͠͸ɺ࠷దԽ΍ޡΓݕग़ͷͨΊɺ͋Δϝιου͕ ϒϩοΫΛऔΔͷ͔Ͳ͏͔Λ੩తʹ൑ఆ͍ͨ͠ɺཧ૝తʹ ͸Ҿ਺ϦετͰ໌͍ࣔͤͨ͞ɺͱ͍͏ಈػ͕͋Δ w ϒϩοΫΛऔΒͳ͍ϝιουʹϒϩοΫΛ౉ͯ͠΋ɺϝ ιουଆͰνΣοΫ͠ͳ͍ݶΓΤϥʔ΍ܯࠂʹ͸ͳΒ ͳ͍ w

    ࢖ΘΕΔͱࢥͬͯϒϩοΫΛ౉͕࣮ͨ͠͸࢖ΘΕͣແ ҙຯͱ͍͏ޡΓʹؾ͚ͮͳ͍ɺͱ͍͏໰୊
  15. ແϒϩοΫQSPDͷஈ֊తഇࢭ w ͨͩɺ$3VCZʹ͸௕͍ؒɺdef method(&block)ͱϒϩοΫΛҾ਺Ͱ ड͚औΔͷ͸஗͍ͱ͍͏໰୊͕͋Γɺख़࿅޻͸ॻ͍ͯ͘Εͳ͔ͬͨ w yield͸ɺ౉͞ΕͨϒϩοΫΛ௚઀ݺͿ໋ྩʹίϯύΠϧ͞ΕΔ w ҰํɺԾҾ਺Ϧετʹ&blockͱॻ͘ͱɺϒϩοΫ͔Β1SPDΦϒδΣΫ τΛੜ੒্ͨ͠ͰɺϩʔΧϧม਺ʹ୅ೖ͞ΕΔ

    w ͞Βʹblock.callͱ͢ΔͱϩʔΧϧม਺Λࢀর͠ɺ1SPDΦϒδΣΫ τʹରͯ͠ϝιου͕ݺͼग़͞ΕΔ w Կεςοϓ΋ଟ͍ʜ
  16. ແϒϩοΫQSPDͷஈ֊తഇࢭ w 3VCZͰɺԾҾ਺Ϧετʹ&blockͱॻ͍ͯ΋ɺ࣮ࡍʹblockΛ࢖͏ ·Ͱ1SPDΦϒδΣΫτͷੜ੒Λ஗Ԇ͢ΔΑ͏ʹͳͬͨ w தͰblock_given?ͱyieldΛ࢖ͬͨ··ɺϒϩοΫΛऔΔ͜ͱΛԾ Ҿ਺ϦετͰදݱͯ͠΋ੑೳ্ͷϖφϧςΟ͕ͳ͘ͳͬͨ w 3VCZͰɺblock.call͸yieldͱಉ౳ͷੑೳʹͳͬͨ w

    ͨͩɺcallͷΦʔόʔϥΠυ༗ແΛ֬ೝ͢ΔͳͲ໘౗ͳ࣮૷ͷࣀ෺ w ͜ΕͰɺ໌ࣔతʹ&blockͱblock.callΛ࢖͏ελΠϧΛਪ঑Ͱ͖ΔΑ ͏ʹͳͬͨ
  17. ແϒϩοΫQSPDͷஈ֊తഇࢭ w ٯʹɺʮϒϩοΫΛऔΒͳ͍͜ͱʯΛ໌ࣔͰ͖ΔΑ͏ʹ͢΂͖͔ɺͱ͍ ͏ٞ࿦΋ਐΜͰ͍Δ w ౰ॳ͸ɺdef foo(&nil)ͱॻ͘͜ͱʹ͢Ε͹͍͍ͷͰ͸ɺͱ·ͱ ·Γ͔͚ͨ w ͔͠͠ɺͦ͏͢ΔͱʮϒϩοΫΛऔΒͳ͍ϝιου͢΂ͯʹ&nilΛ

    ೖΕΑ͏ʯͱ͍͏େྔͷ13͕Ұؾʹԡ͠دͤΔةዧ͕͋ΔͷͰऔ Γ΍Ίʹ w ݱࡏ͸ɺίϯύΠϧ࣌ͷ੩తղੳͰͰ͖ͳ͍͔ɺͱ͍͏औΓ૊Έ͕ࢼ͞ Ε͍ͯΔ
  18. ແϒϩοΫQSPDͷஈ֊తഇࢭ w ͨͩɺTVQFSͱ͍͏น͕ൃݟ͞ΕΔ w super͋Δ͍͸super()ͱॻ͘ͱɺԿ΋ࢦఆ͍ͯ͠ͳ͍͕ɺ ౉͖ͬͯͨϒϩοΫΛεʔύʔϝιουʹ౉͢Α͏ʹͳ͍ͬͯΔ w superͷํ͸෼͔Δ͕ɺsuper()ͷํ͸Ұମͳͥʜ w ͦͷͨΊɺεʔύʔϝιου΋ḷͬͯݟͳ͍ͱϒϩοΫҾ਺Λ࢖ͬ

    ͍ͯΔͷ͔͸Θ͔Βͳ͍ w ݱࡏ΋طଘͷϢʔβίʔυΛؚΊௐࠪɾݚڀ͕ਐߦத$
  19. &OVNFSBCMFUBMMZ w ཁૉΛ஋ผʹ਺͑Δϝιου • ["a", "b", "b", "c", "a", "a"].tally


    # => { "a" => 3, "b" => 2, "c" => 1 } • SQLͷGROUP BY+COUNT()ͷΑ͏ͳ΋ͷ • ࠓ·Ͱ͸group_byΛ࢖ͬͯ͜Μͳ෩ʹͤ͟ΔΛಘͳ͔ͬͨॲཧ • ["a", "b", "b", "c", "a", "a"]
 .group_by(&:itself)
 .transform_values(&:size)
  20. &OVNFSBCMFUBMMZ w ౰ॳ͸count_byͱ͍͏໊લͰɺϒϩοΫΛऔΔܗͰఏҊ͞Ε͍ͯͨʢϒϩοΫΛద ༻ͨ͠஋Ͱूܭ͢Δʣ w ͔͠͠ɺ͍͔ͭ͘ͷ೉఺ͰࢍಉΛಘΒΕͣ w ໊લ͕͍·͍ͪcountͱͷରশੑ͕ͳ͍ʢsort_bysort max_bymaxͳ Ͳ͔Βͷྨਪ͕ޮ͔ͳ͍ʣ

    w ػೳ͕ଟ͍ϒϩοΫΛऔΔͷ͸ΦϓγϣφϧͰ͸ʁඞཁͳΒmap͔ͯ͠Β౉ ͤ͹͍͍ w γϡϫϧπม׵͍ͨ͜͠ͱ΋͋Δ͔΋͠Εͳ͍͕ɺ୅ද஋ΛͲ͏͢Δ͔࢓ ༷ΛܾΊ೉͍ w tallyͱ͍͏໊લΛಘͯɺϒϩοΫΛऔΒͳ͍γϯϓϧͳܗͰ࠾༻
  21. &OVNFSBCMFUBMMZ w UBMMZͱ͸਺Λ਺͑ͯه࿥͢Δ͜ͱͰɺࢴ΍ࠇ൘ʹҰຊͣͭઢΛॻ ͖ͳ͕ΒΧ΢ϯτ͢ΔͨΊͷه߸ͷݺশͰ΋͋Δʢ೔ຊͰͷʮਖ਼ʯ ʹ͋ͨΔه߸ɺʮ㠟ʯͷΑ͏ͳܗʣ w ͪͳΈʹ6OJDPEFͰʮਖ਼ʯͱͱ΋ʹೖΓ·ͨ͠
 IUUQTUXJUUFSDPNLFO@MVOEFTUBUVT 

  22. ٫Լ͞Εͨ΋ͷ w BVUPMPBEͷഇࢭ⚡ w .BU[͸εϨουηʔϑςΟͷ؍఺͔Βຊ࣭తʹةͳ͍ͷͰͣͬͱফͨ͠ ͍೿ w 3BJMT͕DPOTU@NJTTJOHͰͷࣗಈϩʔυػߏΛ΍ΊͯBVUPMPBEΛ࢖͏ ;FJUXFSLΛͰ࠾༻ɺͱ͍͏͜ͷλΠϛϯάͰ͸͠͝Λ֎͢ͷ͔⽐ͱ ࢲͷதͰ૽વ

    w ͔͠͠3BJMTΞϓϦͷ։ൃͰ͸FBHFSMPBEJOHͩͱϓϩηεىಈɾ࠶ىಈ ͕ॏ͗͢ΔͷͰ͜͏͍͏ػߏ͸ඞਢͱઆಘ☝ w গͳ͘ͱ΋3VCZYͷؒ͸࢒͢ɺͱ͍͏.BU[ͷݴ࣭Λಘͨ
  23. ٫Լ͞Εͨ΋ͷ w BVUPMPBEͷഇࢭ w ͦͷޙɺBVUPMPBEΛΑΓ҆શʹ͢ΔͨΊͷݚڀͱվम͕ਐΜͰ ͍Δ w IUUQXXXBLSPSHEIUNMB@@ w IUUQXXXBLSPSHEIUNMB@@@

    w IUUQTCVHTSVCZMBOHPSHJTTVFT w IUUQTCVHTSVCZMBOHPSHJTTVFT
  24. ٫Լ͞Εͨ΋ͷ w "SSBZ΍)BTI΋ɺ4USJOHͷΑ͏ʹσϑΥϧτͰϑϦʔζ͢ΔϞʔυ͕΄͠ ͍ɺ͋Δ͍͸୯߲ ͰEVQGSFF[F͍ͨ͠ w IUUQTCVHTSVCZMBOHPSHJTTVFT w ʮͦΜͳʹ΄͍͠ʁʯ·ͩջٙత w

    Ͱ΋3VCPDPQར༻ऀ͔Β͢Δͱɺఆ਺഑ྻɾϋογϡʹ.freezeΛ෇ ͚ͯճΒΕΔܦݧ͸ۤʑ͍͠ʜ w ͍ͣΕ·ͨνϟϯε͋Δ͔΋
  25. ։ൃதɾݕ౼தͷػೳ w #FHJOMFTT 4UBSUMFTT 3BOHFT w IUUQTCVHTSVCZMBOHPSHJTTVFT w &OEMFTT3BOHFT @ary[1..]

    ͕ೖͬͨͷͰɺࠓ౓͸ࠨ୺ଆΛলུͨ͠΋ͷ΋ ೖΕ͍ͨ w "DUJWF3FDPSEͳͲͷ%4-ͰศརProduct.where(price: ..1000) w &OEMFTT3BOHFT΋ಋೖޙʹ͍͔ͭ͘໰୊͕ൃݟɾରॲ͞ΕͨͷͰɺͦΕΒͷ ख౰͕ͯऴΘͬͯམͪண͍͔ͯΒ w &OVNFSBUPS:JFMEFSUP@QSPD w IUUQTCVHTSVCZMBOHPSHJTTVFT
  26. ։ൃதɾݕ౼தͷػೳ w σϑΥϧτϒϩοΫҾ਺@1 @2 ʜ w IUUQTCVHTSVCZMBOHPSHJTTVFT w ͣͬͱ๬·Ε͍ͯΔػೳnumbers.map {

    "%02x" % @1 } w طଘจ๏ͱিಥͷͳ͍ح੻ͷҊʢ@1 @2,…ʣʹظ଴ w ύλʔϯϚονػೳʢDBTFJOߏจʣ w IUUQTCVHTSVCZMBOHPSHJTTVFT w ڧ͘଴ͪ๬·Ε͖ͯͨػೳ w εΫϦϓτݴޠͷதͰ͸ڧྗͰ3VCZͷڧΈͱݴΘΕ͖͕ͯͨɺؔ਺ܕݴޠ͕ϝδϟʔ ʹͳ͖ͬͯͨࠓͰ͸ͪΐͬͱݟྼΓ͢Δ
  27. ։ൃதɾݕ౼தͷػೳ w &4ελΠϧͷϋογϡ؆ศه๏ʢpFMEQVOOJOHʣ w IUUQTCVHTSVCZMBOHPSHJTTVFT w {
 name: name,
 type:

    type,
 created_at: created_at
 }
 ↓
 { name, type, created_at }
 
 ͳͲͱॻ͖͍ͨͳɺͱ͍͏ྫͷ΍ͭ w .+*5ʹΑΔ͞ΒͳΔߴ଎Խ
  28. ࠓޙͷ3VCZͷ༧ఆ w ೥݄ w ೔ʹ։ൃऀձٞIUUQTCVHTSVCZMBOHPSHJTTVFT w 6OJDPEF͕ग़ͨΒɺ3VCZYΛϦϦʔε w ೥݄ w

    3VCZ,BJHJձظલ೔ʹ։ൃऀձٞ w ৽ݩ߸͕ൃද͞ΕͨΒ6OJDPEF͕ग़Δ w ͦΕΛ଴ͬͯ3VCZZΛϦϦʔε w ຖ݄։ൃऀձٞ w νϟϯε͸ຖ݄ IUUQTCVHTSVCZMBOHPSHJTTVFT w ೥݄ w ೔3VCZϦϦʔε