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

Beyond the RuboCop Defaults

Koichi ITO
September 20, 2024

Beyond the RuboCop Defaults

Rubyセミナー 東京 (https://www.ruby.or.jp/ja/news/20240702)

Koichi ITO

September 20, 2024
Tweet

More Decks by Koichi ITO

Other Decks in Programming

Transcript

  1. w ൃද࣌ͷ࠷৽ͷ3VCP$PQΛલఏʹ͠ ͯ·͢ ຊτʔΫʹ͓͚Δόʔδϣϯ $ ruby -v ruby 3.4.0dev (2024-09-19T18:51:21Z

    master 2230ac4a28) +PRISM [x86_64-darwin23] $ bundle exec rubocop -V 1.66.1 (using Parser 3.3.5.0, rubocop-ast 1.32.3, running on ruby 3.4.0) [x86_64-darwin23] ࠓ೔͸όʔδϣϯґଘ௿Ίͷߟ͑ํΛத৺ͱͨ͠࿩Ͱ͕͢೦ͷҝ
  2. w 3VCP$PQ͸3VCZ4UZMF(VJEFͷϥϯλΠϜ࣮૷ w ॳظʹ໨ࢦͨ͠ͷ͸HPGNUతͳେ౷ҰϑΥϚολʔ w 3VCZίϛϡχςΟʹ͸ڝ߹͢ΔελΠϧ΍޷Έ͕ ͋Δ͜ͱʹؾͮ͘ FH จࣈྻͷΫΥʔτ 

    w 3VCZ4UZMF(VJEFΛσϑΥϧτͱͯ͠ɺҰൠతͱ ࢥΘΕΔελΠϧΛߏ੒Մೳʹ͢ΔΑ͏ʹͳΔ 3VCP$PQͷ఩ֶαϚϦʔ https://rubystyle.guide/
  3. %JTBCMFE#Z%FGBVMUUSVF 3VCP$PQͷσϑΥϧτߏ੒ SE1BSUZͷϓϦηοτ ϧʔϧ͕ݫ͘͠ͳΔ طଘίʔυ͕͋Δͱ͖ͷಋೖͷ೉қ౓ ҧ൓͕ݕ஌͞ΕΔਪఆྔ ࣗ෼ͨͪͷϧʔϧ ؾʹೖΒͳ͍ϧʔϧΛ ΧελϚΠζ͢Δ ͋ͱͰߏ੒͢Δ

    ྫ͑͹1VMM3FRVFTUͰͷϨϏϡʔʹ ج͍ͮͯಉ͡ࢦఠ͕ͳ͍Α͏ߏ੒͢Δ %3: ⭐ ⭐ ⭐ ⭐ελʔτ஍఺ SEQBSUZ੡ͷϓϦηοτ ͷ··Ͱ΋ྑ͍͠ߏ੒ ͯ͠΋ྑ͍ ࠷খߏ੒Ͱͷ࢝Ίํͷྫ 4UBOEBSE3VCZ ߏ੒ෆՄ  SVCPDPQSBJMT@DPO fi H ߏ੒Մ  SVCPDPQSBJMTPNBLBTF ߏ੒Մ
  4. EJTBCMF UPEP ίϝϯτͷτϨʔυΦϑ def complex_method # rubocop:disable Metrics/AbcSize complex_code ...

    end w ߦ຤ίϝϯτ͸FOBCMFίϝϯτ͕ෆཁ͕ͩɺHJUCMBNFʹऑ͍ EJTBCMFίϝϯτΛফͤΔݟཱ͕ͯͳ͍৔߹ʹ࢖͑Δɻ ࣮૷͕มΘΓͦ͏ͳ"CD4J[Fʹ͸ෆ޲͖ɻ # rubocop:todo Metrics/AbcSize def complex_method complex_code ... end # rubocop:enable Metrics/AbcSize w ൣғίϝϯτ͸HJUCMBNFʹڧ͍൓໘ͰFOBCMFίϝϯτ͕ඞཁ EJTBCMFίϝϯτΛফͤΔݟཱ͕ͯ͋Δ৔߹ʹ༗༻ɻ ಛʹҙࢤΛ͍ࣔͨ͠৔߹͸SVCPDPQUPEPͰ໌ࣔɻ
  5. w rubocop -aΦϓγϣϯ͸ΦʔτίϨΫτʹ Αͬͯඇޓ׵͕ੜ͡ͳ͍ ҆શͳ $PQʹݶఆ͠ ࣮ͯߦ͢Δ w rubocop -AΦϓγϣϯ͸ɺِཅੑɺҙਤత

    ʹΑΒͣৼΔ෣͍͕มΘΓ͏Δɺ$PQΛؚΉ͢ ΂ͯͷ ҆શͰͳ͍ ΦʔτίϨΫτΛ࣮ߦ͢Δ छྨͷΦʔτίϨΫτ
  6. $ rails new example && cd $_ $ rails new

    example && cd $_ $ cat Gemfile | grep rubocop # Omakase Ruby styling [https://github.com/rails/rubocop gem "rubocop-rails-omakase", require: false 3BJMTͰͷSBJMTOFX $ rails new example && cd $_ $ cat Gemfile | grep rubocop # Omakase Ruby styling [https://github.com/rails/rubocop gem "rubocop-rails-omakase", require: false $ cat .rubocop.yml # Omakase Ruby styling for Rails inherit_gem: { rubocop-rails-omakase: rubocop.yml } # Overwrite or add rules to create your own house style # # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]` # Layout/SpaceInsideArrayLiteralBrackets: # Enabled: false
  7. %JTBCMFE#Z%FGBVMUUSVF 3VCP$PQͷσϑΥϧτߏ੒ SE1BSUZͷϓϦηοτ ϧʔϧ͕ݫ͘͠ͳΔ طଘίʔυ͕͋Δͱ͖ͷಋೖͷ೉қ౓ ҧ൓͕ݕ஌͞ΕΔਪఆྔ ࣗ෼ͨͪͷϧʔϧ ؾʹೖΒͳ͍ϧʔϧΛ ΧελϚΠζ͢Δ ͋ͱͰߏ੒͢Δ

    ྫ͑͹1VMM3FRVFTUͰͷϨϏϡʔʹ ج͍ͮͯಉ͡ࢦఠ͕ͳ͍Α͏ߏ੒͢Δ %3: ⭐ ⭐ ⭐ ⭐ελʔτ஍఺ SEQBSUZ੡ͷϓϦηοτ ͷ··Ͱ΋ྑ͍͠ߏ੒ ͯ͠΋ྑ͍ ࠷খߏ੒Ͱ࢝ΊΔબ୒ࢶͷͻͱͭ 4UBOEBSE3VCZ தྔڃ  SVCPDPQSBJMT@DPO fi H தྔڃ  SVCPDPQSBJMTPNBLBTF ܰྔڃ
  8. $ rails new example && cd $_ $ cat Gemfile

    | grep rubocop # Omakase Ruby styling [https://github.com/rails/rubocop gem "rubocop-rails-omakase", require: false $ cat .rubocop.yml # Omakase Ruby styling for Rails inherit_gem: { rubocop-rails-omakase: rubocop.yml } # Overwrite or add rules to create your own house style # # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]` # Layout/SpaceInsideArrayLiteralBrackets: # Enabled: false 3BJMTͷPNBLBTF͸Ͳ͏ʁ Ͳ͏ͳΜͩΖ͏͔ʁͱ͍͏ σϑΥϧτ͚ͩΕͲɺ Ϣʔβʔ΁ઃఆΛଅͨ͢Ί Θ͟ͱ΍ͬͯΔʁ σϑΥϧτ
  9. CJOSBJMTHͰίʔυੜ੒࣌ʹࣗಈमਖ਼ - #config.generators.apply_rubocop_autocorrect_after_generate! + config.generators.apply_rubocop_autocorrect_after_generate! w DPO fi HFOWJSPONFOUTEFWFMPQNFOUSCͰ ίϝϯτΞ΢τ͞Ε͍ͯΔ

    BQQMZ@SVCPDPQ@BVUPDPSSFDU@BGUFS@HFOF SBUFΛ༗ޮʹ͢ΔCJOSBJMTHͨ͠ίʔυʹ ରͯ͠ΦʔτίϨΫτద༻ͱͳΔ SBJMTSBJMTʹσϑΥϧτແޮͷཧ༝͸ৄ͍͕͠جຊ༗ޮͰྑ͍ͱࢥ͏
  10. w ·ͭ΋ͱ͞Μ؂म ͷϓϦηοτ w 3VCZͷ਌ͱ͍͏͜ͱ͸Ѫ৘͸ੈքҰ   w DisabledByDefault: trueϕʔεͷ֦ுHFN

    w ϧʔϧ $PQ ͸ແݶʹ૿͍͍͑ͯͬͯΔͷͰɺ ϛχϚϜͷελʔλʔΩοτΑ͏ͳ΋ͷʹͳΓ ͦ͏ .BU[$PQ͕બ୒Ͱ͖Δͱೲಘʁ