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

What's Cooking In Ruby 2.7

Akinori MUSHA
February 27, 2019

What's Cooking In Ruby 2.7

Akinori MUSHA

February 27, 2019
Tweet

More Decks by Akinori MUSHA

Other Decks in Technology

Transcript

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

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

    4FUͷ࣮૷ w CJHEFDJNBM TZTMPH JQBEES [MJCͳͲͷΠϯϙʔτࢧԉ΍อक w 4IFMMXPSETʹTIFMMFTDBQF TIFMMKPJOΛ࣮૷ w 3VCZͷϦϦʔε
  3. obj.:meth w obj.method(:meth)ͷ୹ॖจ๏ w ϝιουΦϒδΣΫτΛऔΓग़͢ w ैདྷͷ0CKFDUNFUIPE͸ϝιουݺͼग़ͩͬͨ͠ w ϝιουΛऔΔͨΊʹϝιουΛݺͿඞཁ͕͋Δʜ
 ͍͔ʹ΋3VCZΒ͍͚͠Ͳʜ

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

    ΦϒδΣΫτͷૉੑΛ໰ΘͣɺϝιουΛऔΓग़͢୹͍ه๏͕΄͍͠ • ͍͔ͭ͘Ҋ͕͕͋ͬͨɺطଘจ๏ͱͷিಥ͕ͳ͘ɺγϯϘϧه๏ ͔ΒϝιουΛ࿈૝͠΍͍͢ʮobj.:methʯʹܾఆ
  5. 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[ͷࢦఠʹ ΑΔ
  6. ແϒϩοΫQSPDͷஈ֊తഇࢭ w ϝιουఆٛதͰϒϩοΫͳ͠ͷQSPDʹΑͬͯϝιουʹ౉͖ͬͯͨϒϩοΫΛ औಘ͢Δ͜ͱ͕Ͱ͖Δ • def each_key
 keys.each(&proc)
 end •

    proc͸Proc.newͱॻ͍ͯ΋ಉ͡ɺଞʹlambda΋͋Δ • block_given?ͰϒϩοΫ͕౉͞Ε͔ͨͲ͏͔Λ൑ఆͰ͖ɺyieldͰݺͼग़ͤ Δͷͱ߹Θͤͯɺ໌ࣔతʹҾ਺Λॻ͔ͳͯ͘΋౉͞ΕͨϒϩοΫΛར༻Ͱ͖ͨ w ͜ͷػೳΛഇࢭ͠Α͏ͱ͍͏ํ޲ʹͳͬͨ w ແϒϩοΫprocͱProc.new͸͔ΒܯࠂɺແϒϩοΫlambda͸ഇࢭ
  7. &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)
  8. &OVNFSBCMFUBMMZ w ౰ॳ͸count_byͱ͍͏໊લͰɺϒϩοΫΛऔΔܗͰఏҊ͞Ε͍ͯͨʢϒϩοΫΛద ༻ͨ͠஋Ͱूܭ͢Δʣ w ͔͠͠ɺ͍͔ͭ͘ͷ೉఺ͰࢍಉΛಘΒΕͣ w ໊લ͕͍·͍ͪcountͱͷରশੑ͕ͳ͍ʢsort_bysort max_bymaxͳ Ͳ͔Βͷྨਪ͕ޮ͔ͳ͍ʣ

    w ػೳ͕ଟ͍ϒϩοΫΛऔΔͷ͸ΦϓγϣφϧͰ͸ʁඞཁͳΒmap͔ͯ͠Β౉ ͤ͹͍͍ w γϡϫϧπม׵͍ͨ͜͠ͱ΋͋Δ͔΋͠Εͳ͍͕ɺ୅ද஋ΛͲ͏͢Δ͔࢓ ༷ΛܾΊ೉͍ w tallyͱ͍͏໊લΛಘͯɺϒϩοΫΛऔΒͳ͍γϯϓϧͳܗͰ࠾༻
  9. ٫Լ͞Εͨ΋ͷ w BVUPMPBEͷഇࢭ⚡ w .BU[͸εϨουηʔϑςΟͷ؍఺͔Βຊ࣭తʹةͳ͍ͷͰͣͬͱফͨ͠ ͍೿ w 3BJMT͕DPOTU@NJTTJOHͰͷࣗಈϩʔυػߏΛ΍ΊͯBVUPMPBEΛ࢖͏ ;FJUXFSLΛͰ࠾༻ɺͱ͍͏͜ͷλΠϛϯάͰ͸͠͝Λ֎͢ͷ͔⽐ͱ ࢲͷதͰ૽વ

    w ͔͠͠3BJMTΞϓϦͷ։ൃͰ͸FBHFSMPBEJOHͩͱϓϩηεىಈɾ࠶ىಈ ͕ॏ͗͢ΔͷͰ͜͏͍͏ػߏ͸ඞਢͱઆಘ☝ w গͳ͘ͱ΋3VCZYͷؒ͸࢒͢ɺͱ͍͏.BU[ͷݴ࣭Λಘͨ
  10. ։ൃதɾݕ౼தͷػೳ 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
  11. ։ൃதɾݕ౼தͷػೳ w σϑΥϧτϒϩοΫҾ਺@1 @2 ʜ w IUUQTCVHTSVCZMBOHPSHJTTVFT w ͣͬͱ๬·Ε͍ͯΔػೳnumbers.map {

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

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

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