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

Block_is_everywhere.pdf

 Block_is_everywhere.pdf

110cd4dcaed019b3bd9809e541afdeb6?s=128

Masafumi Okura

December 11, 2019
Tweet

Transcript

  1. Block is everywhere Event: LegalForce Ruby Meet Up #1 Date:

    2019-12-11 Speaker: େ૔խ࢙
  2. pp self • ໊લɿେ૔խ࢙ • ॴଐɿϑϦʔϥϯε • ΤσΟλɿVim • Α͍͘ΔίϛϡχςΟɿGrow.rbʢओ࠵ʣɺTama.rbͳͲ

  3. RubyͷϒϩοΫ͸ૉ੖Β͍͠

  4. RubyͷϒϩοΫͷ࢖ΘΕํ

  5. RubyͷϒϩοΫ͕࢖ΘΕ͍ͯΔՕॴ • RailsͷϨΠΞ΢τͷϨϯμϦϯάʢyieldʣ • ֤छgemͷઃఆʢconfigure doʣ • Rake΍FactoryBotʹ୅ද͞ΕΔDSL • Ϧιʔεͷ؅ཧ

    • ϑοΫͷఆٛ • etc…
  6. RailsͷϨΠΞ΢τͷϨϯμϦϯά • ϨΠΞ΢τϑΝΠϧͷதʹ͋Δ”yield”͸࣮͸ϒϩοΫΛධՁ͠ ͍ͯΔ • RailsͷϏϡʔʢςϯϓϨʔτʣ͸࠷ऴతʹ͸Rubyͱ࣮ͯ͠ߦ͞ ΕΔͷͰೖΕࢠΛ࣮ݱ͢ΔͷʹϒϩοΫΛωετ͍ͤͯ͞Δͱ ࢥΘΕΔ • Ұ൪ແҙࣝʹϒϩοΫΛ࢖͍ͬͯΔՕॴͰ͸ʁ

  7. ֤छgemͷઃఆ • “configure do |config|”ͱ͔ͷΞϨ͸౰વϒϩοΫΛ࢖͍ͬͯΔ • ίϯϑΟάΦϒδΣΫτʹΞΫηε͢Δͷ͕௚ײతʹͰ͖Δ • ઃఆ͞ΕΔଆ͸”config =

    Config.new; yield(config)”Έ͍ͨʹ ॻ͚Δ • ࣮͸ϒϩοΫΛ࢖͏ඞཁ͸ͳ͍…ʁ
  8. DSL • DSLΛ࣮૷͢ΔͷʹϒϩοΫʢͱ͍͏ΑΓProcΦϒδΣΫτ ͱ”instance_eval”ϝιουʣ͸ඞਢ • RSpecͩͱϒϩοΫΛ࣮ߦ͢Δͨͼʹಗ໊ΫϥεΛ࡞੒͍ͯͨ͠ Γ΋͢Δ • ʮϝλϓϩάϥϛϯάRubyʯͱ͍͏໊ஶΛಡΉͱ࢓૊Έ͕Θ͔Δ

  9. Ϧιʔεͷ؅ཧ • “File.open do |f|”͕୅දత • “Net::HTTP.start do |http|”ͳΜ͔΋ͦ͏ •

    ϝιου͕ϦιʔεΛ࡞੒͠ɺϒϩοΫ͕ด͡ΔͱϦιʔε΋ ด͡ΒΕΔ
  10. ϑοΫͷఆٛ • RSpecͷ”before(:each)”΍Railsͷ”before_action”ͳͲ͕୅දత • ϒϩοΫͷத਎ΛProcΦϒδΣΫτͱͯ࣋ͪ͠ճ͢ • aroundܥͷϑοΫ͸಺෦ͰyieldΛݺͿͱຊॲཧ͕૸ΔͷͰೋ ॏʹϒϩοΫΛ࢖͍ͬͯΔ

  11. ଞʹ΋৭ʑͳϝιου͕ϒϩοΫΛड͚Δ • Enumerable#countʢϒϩοΫ͕trueͷཁૉΛ਺͑Δʣ • ActiveRecord::Relation#buildʢϒϩοΫ಺ͰΠϯελϯεઃఆʣ • Hash.newʢॳظԽͷํ๏Λنఆʣ • String#gsubʢஔ׵ͷํ๏Λنఆʣ

  12. ͓·͚

  13. RubyͷϒϩοΫ͸Ϋϩʔδϟ

  14. पғͷ؀ڥʹΞΫηεͰ͖ΔͷͰɺ ৭ʑͱϋοΫ͢Δ͜ͱ͕Ͱ͖Δ

  15. ྫɿΧ΢ϯλΦϒδΣΫτ