Improve Ruby coding styles and Lint

Improve Ruby coding styles and Lint

RubyKaigi 2018
http://rubykaigi.org/2018

023b04c98f39cc041293d780352432ff?s=128

Koichi ITO

June 01, 2018
Tweet

Transcript

  1.  'SJ Improve Ruby coding style rules and Lint ,PJDIJ*50FTN

    3VCZ,BJHJ 4FOEBJ*OUFSOBUJPOBM$FOUFS)BHJ How do you join social coding?
  2. !LPJD

  3. QPTJUJPOUBML

  4. :PVNBZLOPX BGBNPVTMBSHF 044DPNNVOJUZ

  5. None
  6. None
  7. *U`TNF

  8. None
  9. w*NQSPWFUFTUDPWFSBHFGPS0SBDMF FOIBODFEBEBQUFS w*OUSPEVDF'SP[FO4USJOH-JUFSBMT w4VQQSFTTXBSOJOHTXJUI&EHF3VCZ w*NQPSUFYQPSU3VCP$PQ DPOpHVSBUJPOT w/JUQJDLTBOETPPO .ZXPSLTJO3BJMT

  10. 3BJMTDPNNVOJUZ JTPOFPGIPNF JONZIFBSU

  11. *PGUFOIBWFBDUJWJUJFT JO3VCP$PQSFDFOUMZ

  12. None
  13. None
  14. w'JYCVHT w$SFBUFOFXDPQT w*NQSPWFEPDVNFOUT w*NQSPWFJOUFSOBMBGGBJST w.BJOUFOBODF$*DPOEJUJPOT .ZXPSLTJO3VCP$PQ

  15. None
  16. FTN

  17. w%JSFDUJPOPGB3BJMTBQQQSPKFDU w6QHSBEJOH3VCZ3BJMTGPSBQQT BOETVSSPVOEJOHHFNT w0SHBOJ[F3BJMT044QBUDI NFFUVQTJOFTN w3FDSVJUJOHJOUFSWJFXFS .ZXPSLTJOFTN

  18. "TUPSZBGUFSZFBST GSPNBNFNPSJBM QSFTFOUBUJPOJO 5PEBZ`TUBML 

  19. 8IBUIBQQFOFE JO 

  20. IUUQKQSVCZJTUOFU3VCZ,BJHJ DQMVHJOQMVHJOBUUBDI@EPXOMPBEQ1SPHSBNpMF@OBNFUIF@JTMBOE@PG@SVCZ@KQEG 3VCZ,BJHJ %BWF5IPNBT

  21. w7BSJPVTDPNQBOJFTVTJOH3VCZ 3BJMTGPSEFWFMPQNFOUBQQFBSFE w(JU)VCIBTBQQFBSFE BOE JOEJWJEVBMBDUJWJUJFTBSFESBXJOH BUUFOUJPO w3VCZJTUTBOE3VCZJTIDPNQBOJFT NBLFWBSJPVT3VCZDPEJOHTUZMFT .PEFSOUJNFT

  22. 5IFSFBSFWBSJPVT 3VCZDPEJOHTUZMFT %JWFSTJUZ

  23. None
  24. "3VCZDPEF def badName if something test end end

  25. 3VOXJUIUIFEFGBVMU 3VCP$PQTFUUJOH

  26. None
  27. %FQBSU/BNF$PQ/BNF%FTDSJQUJPO

  28. 3VCP$PQIBTTFWFSBM EFQBSUNFOUT

  29. 0GpDJBMNBOVBM

  30. )FSF

  31. None
  32. -BZPVU%FQBSUNFOU "MJHO"SSBZ .FUBQIFS "MJHO)BTI &MTF"MJHONFOU &NQUZ-JOFT &OE"MJHONFOU &YUSB4QBDJOH *OEFOU"SSBZ -JOU%FQBSUNFOU

    %VQMJDBUF.FUIPET %VQMJDBUFE,FZ &MTF"MJHONFOU 4DSJQU1FSNJTTJPO 6OJpFE*OUFHFS 6OSFBDIBCMF$PEF 6OMFTT"TTJHONFOU .FUSJDT%FQBSUNFOU "CD4J[F #MPDL-FOHUI #MPDL/FTUJOH $MBTT-FOHUI -JOF-FOHUI .FUIPE-FOHUI 1BSBNFUFS-JTUT ʜ
  33. %FQBSU/BNF$PQ/BNF%FTDSJQUJPO

  34. 3VCP$PQIBTNBOZlCVJMUJODPQTz BOElDVTUPNDPQTzZPVDBOBEECZVTJOH QMVHHBCMFNFDIBOJTN

  35. /FXDPQ NBEFCZNF

  36. $SFBUJPODPQDPVOU QXE 6TFSTLPJDTSDHJUIVCDPNSVCPDPQIRSVCPDPQ DBU$)"/(&-0(NEcHSFQLPJDcHSFQJBEEOFXcXDM 

  37. $SFBUJPODPQDPVOU QXE 6TFSTLPJDTSDHJUIVCDPNSVCPDPQIRSVCPDPQ DBU$)"/(&-0(NEcHSFQLPJDcHSFQJ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PQ <!LPJD><>
  45. -JTUPGDPQT*DSFBUFE  "EEOFXA1FSGPSNBODF4PSU8JUI#MPDLADPQUPVTF ATPSU@CZ GPP AJOTUFBEPGATPSU\cB CcBGPPCGPP^A <!LPJD><>

  46. /VNCFSPG PQQPSUVOJUJFTUP NBLFDPQT ()SVCPDPQIRSVCPDPQ 

  47. /VNCFSPG PQQPSUVOJUJFTUP NBLFDPQT ()SVCPDPQIRSVCPDPQ 3VCZ  

  48. /VNCFSPG PQQPSUVOJUJFTUP NBLFDPQT ()SVCPDPQIRSVCPDPQ 3VCZ .ZQSPKFDUT   

  49. /VNCFSPG PQQPSUVOJUJFTUP NBLFDPQT ()SVCPDPQIRSVCPDPQ 3VCZ .ZQSPKFDUT ()SBJMTSBJMT   

    
  50. -FUTUBMLBCPVUUIFCBDLHSPVOEPG NBLJOHUIFTFDPQTBOEUIF LOPXMFEHFHBJOFEJOUIBUQSPDFTT

  51.  'SJ Improve Ruby coding style rules and Lint ,PJDIJ*50FTN

    3VCZ,BJHJ 4FOEBJ*OUFSOBUJPOBM$FOUFS)BHJ How do you join social coding?
  52. "HFOEB 0OUIFEJGGFSFODF CFUXFFODPEJOHTUZMFBOE -JOU &NVMBUF3VCZ`TXBSOJOH  /FXDPQTUBSUTXJUI
 ZPVSQSPKFDU $SPTTPWFS
 044DPNNVOJUJFT

    1BSU 1BSU 1BSU
  53. 1BSU

  54. 0OUIFEJGGFSFODF CFUXFFODPEJOH TUZMFBOE-JOU

  55. 4UZMF -JOU

  56. 4UZMF

  57. w4UZMFJTGSPN WBSJPVTDVMUVSFT w6OJpFEXJUIJOB QSPKFDUTQFDJpD UBTUF 4UZMF

  58. 5IFSFT.PSF5IBO 0OF8BZ5P
 %FpOFB4UZMF

  59. w.JOFSP"PLJTDPEJOHTUZMF +"  wIUUQJMPWFSVCZOFUKBSVCZDPEJOHTUZMFIUNM w4IVHP.BFEBTDPEJOHTUZMF +"  wIUUQTTIVHPOFUSVCZDPEFDPOW DPEFDPOWI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% cat example.rb require ‘erb' ERB.new('hi', nil, '-', ‘@output_buffer')

  80. &3#OFXJO3VCZ % cat example.rb require ‘erb' ERB.new('hi', nil, '-', ‘@output_buffer')

    % ruby example.rb example.rb:3: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. example.rb:3: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead. example.rb:3: warning: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.
  81. &3#OFXJO3VCZ # bad # ERB.new(str, nil, '-', '@output_buffer') # good

    # ERB.new(str, trim_mode: '-', eoutvar:'@output_buffer')
  82. 3VCZ$PNQBUJCMF # https://github.com/ruby/ruby/commit/3406c5d # Ruby 2.6+ if ERB.instance_method(:initialize).parameters.assoc(:key) ERB.new(str, trim_mode:

    '-', eoutvar: '@output_buffer') else ERB.new(str, nil, '-', '@output_buffer') end
  83. NJOJNVN@UBSHFU@SVCZ@WFSTJPO

  84. &NVMBUJPOPG3VCP$PQ % rubocop example.rb --only Lint/ErbNewArguments Inspecting 1 file W

    Offenses: example.rb:3:15: W: Lint/ErbNewArguments: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments. ERB.new('hi', nil, '-', '@output_buffer') ^^^ example.rb:3:20: W: Lint/ErbNewArguments: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: '-') instead. ERB.new('hi', nil, '-', '@output_buffer') ^^^ example.rb:3:25: W: Lint/ErbNewArguments: Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: '@output_buffer') instead. ERB.new('hi', nil, '-', '@output_buffer') ^^^^^^^^^^^^^^^^
  85. %JEZPVIBWFBOZQSPCMFNT

  86. FSJLIVEBUIPS %FQSFDBUFTBGF@MFWFMPG&3#OFXJO3VCZ

  87. None
  88. 5I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QPSUVOJUJFT

  101. 044 3JEFPO044 3JEFPO044

  102. "DPQ*NBEFIFMQFE FWFOJONZGSJFOET QSPKFDU

  103. !POLTBZT l 4UZMF.JYJO6TBHFDPQ
 IBTXPSLFE 
 TP*TIPVMEHJWF!LPJD IUUQTUXJUUFSDPNPOLTUBUVT

  104. )PXUPNBLF4UZMF.JYJO6TFHFDPQ

  105. SBLFOFX@DPQ % be rake new_cop[Rails/TopLevelInclude]

  106. SBLFOFX@DPQ % be rake new_cop[Rails/TopLevelInclude] Files created: - lib/rubocop/cop/rails/top_level_include.rb -

    spec/rubocop/cop/rails/ top_level_include_spec.rb File modified: - `require_relative 'rubocop/cop/rails/ top_level_include'` added into lib/rubocop.rb Do 3 steps: 1. Add an entry to the "New features" section in CHANGELOG.md, e.g. "Add new `TopLevel/Include` cop. ([@your_id][])" 2. Add an entry into config/enabled.yml or config/disabled.yml 3. Implement your new cop in the generated file!
  107. 8SJUFFYBNQMFT # TODO: when finished, run `rake generate_cops_documentation` to update

    the docs module RuboCop module Cop module Rails # TODO: Write cop description and example of bad / good code. # # @example # # bad # bad_method() # # # good # bad_method(args) 8SJUFDPQEFTDSJQUJPOBOE FYBNQMFPGCBEHPPE
  108. 8SJUFTQFDT describe RuboCop::Cop::Rails::TopLevelInclude do it 'registers an offense when using

    `#bad_method`' do expect_offense(<<-RUBY.strip_indent) bad_method ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`. RUBY end it 'does not register an offense when using `#good_method`' do expect_no_offenses(<<-RUBY.strip_indent) good_method RUBY end end 8SJUFBCBEDBTFPOUIFAFYQFDU@P⒎FOTFA NFUIPE
  109. 8SJUFTQFDT describe RuboCop::Cop::Rails::TopLevelInclude do it 'registers an offense when using

    `#bad_method`' do expect_offense(<<-RUBY.strip_indent) bad_method ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`. RUBY end it 'does not register an offense when using `#good_method`' do expect_no_offenses(<<-RUBY.strip_indent) good_method RUBY end end 8SJUFBCBEDBTFPOUIFAFYQFDU@P⒎FOTFA NFUIPE 8SJUFBHPPEDBTFPOUIFAFYQFDU@OP@P⒎FOTFTA NFUIPE
  110. w8JUIexpect_offense BOE expect_no_offenses UIFUFTUDBOCF XSJUUFOSPVHIMZ 4USPOHUFTUJOH%4-

  111. 8SJUFTQFDT BHBJO describe RuboCop::Cop::Rails::TopLevelInclude do it 'registers an offense when

    using `#bad_method`' do expect_offense(<<-RUBY.strip_indent) bad_method ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`. RUBY end it 'does not register an offense when using `#good_method`' do expect_no_offenses(<<-RUBY.strip_indent) good_method RUBY end end 8SJUFBCBEDBTFPOUIFAFYQFDU@P⒎FOTFA NFUIPE 8SJUFBHPPEDBTFPOUIFAFYQFDU@OP@P⒎FOTFTA NFUIPE
  112. 3FXSJUFTQFDT describe RuboCop::Cop::Rails::TopLevelInclude do it 'registers an offense when using

    outside class' do expect_offense(<<-RUBY.strip_indent) include ^^^^^^^ `include` is used at the top level. Use inside `class` or `module`. class C end RUBY end 8SJUFBCBEDBTFPOUIFAFYQFDU@P⒎FOTFA NFUIPE
  113. 3FXSJUFTQFDT describe RuboCop::Cop::Rails::TopLevelInclude do it 'registers an offense when using

    outside class' do expect_offense(<<-RUBY.strip_indent) include ^^^^^^^^^ `include` is used at the top level. Use inside `class` or `module`. class C end RUBY end it 'does not register an offense when using inside class' do expect_no_offenses(<<-RUBY.strip_indent) class C include M end RUBY 8SJUFBHPPEDBTFPOUIFAFYQFDU@OP@P⒎FOTFTA NFUIPE
  114. SVCZQBSTF % ruby-parse -e 'include M’ (send nil :include (const

    nil :M)) % ruby-parse -e 'extend M’ (send nil :extend (const nil :M)) % ruby-parse -e 'prepend M’ (send nil :prepend (const nil :M)) (FU4FYQSFTTJPOGSPNSVCZQBSTFDPNNBOE 3VCP$PQJTEFQFOEFOUPO1BSTFSHFN
  115. "CTUSBDU4ZOUBY5SFF % ruby_ast_visualizer -e 'include M’ (send nil :include (const

    nil :M)) Wrote a.png IUUQTHJUIVCDPNLPJDSVCZ@BTU@WJTVBMJ[FS
  116. EFG@OPEF@NBUDIFS def_node_matcher :include_statement, <<-PATTERN (send nil? ${:include :extend :prepend} (const

    nil? _)) PATTERN 3FXSJUFBTOPEFQBUUFSOCBTFEPO4FYQSFTTJPO 'VSUIFSSFBEJOH IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCNBTUFSMJCSVCPDPQOPEF@QBUUFSOSC
  117. 3VCP$PQ/PEF1BUUFSO (send nil? :include (const nil? _)) (send nil? :extend

    (const nil? _)) (send nil? :prepend (const nil? _)) (send nil? ${:include :extend :prepend} (const nil? _)) 6TFA\AA^A NBUDIFTJODMVEF FYUFOEPSQSFQFOE "OEAA BSCJUSBSZNBUDIJOHDBOCFQFSGPSNFEPOB DBQUVSF 'VSUIFSSFBEJOH IUUQTHJUIVCDPNSVCPDPQIRSVCPDPQCMPCNBTUFSMJCSVCPDPQOPEF@QBUUFSOSC
  118. PO@YYYBEE@PGGFOTF MSG = '`%<statement>s` is used at the top level.'

    \ 'Use inside `class` or `module`.'.freeze def_node_matcher :include_statement, <<-PATTERN (send nil? ${:include :extend :prepend} (const nil? _)) PATTERN def on_send(node) return unless (statement = include_statement(node)) return unless top_level_node?(node) add_offense( node, message: format(MSG, statement: statement) ) end 8SJUFFWFOUIBOEMFSAPO@OPEF@OBNFA PSAJOWFTUJHBUFA NFUIPE
  119. *NQMFNFOUBUJPO 3FE (SFFO 3FGBDUPS 5FTUDPEFJTNPSFPCWJPVTBOEFBTJFSUP VOEFSTUBOEUIBOUIFPQFSBUJPOPG"45

  120. 8SJUFDPOpHpMFT Rails/TopLevelInclude: Description: 'Checks that `include`, `extend` and `prepend` exists

    at the top level.' Enabled: true # In this PR, the following setting has been removed Rails/TopLevelInclude: Include: - app/**/*.rb "EEBOFOUSZJOUPDPOpHFOBCMFEZNM PSDPOpHEJTBCMFEZNM "EEBOFOUSZJOUPDPOpHEFGBVMUZNM *GEFGBVMUTFUUJOHJTSFRVJSFE
  121. SBLFHFOFSBUF@DPQT@EPDVNFOUBUJPO % # Write the CHANGELOG.md % be rake generate_cops_documentation

    % be rake # be rake parallel:spec Finished in 39.07 seconds (files took 5.91 seconds to load) 17739 examples, 0 failures, 1 pending # be rake parallel:ascii_spec Finished in 37.04 seconds (files took 5.26 seconds to load) 17739 examples, 0 failures, 1 pending # be rake internal_investigation 1046 files inspected, no offenses detected 8IFOpOJTIFE SVO`rake generate_cops_documentation`UPVQEBUFUIFEPDT
  122. 0QFOB13

  123. $POWFSTBUJPO

  124. $POWFSTBUJPO

  125. $POWFSTBUJPO

  126. $POWFSTBUJPO

  127. %JEZPVOPUJDFJU !QPDLF

  128. $POWFSTBUJPO

  129. SVCPDPQIRSVCPDPQ "EEOFXA4UZMF.JYJO6TBHFADPQ

  130. None
  131. )FSF

  132. 3VCP$PQIBTOPQSFSFMFBTF

  133. None
  134. Feedback

  135. w'BMTFQPTJUJWFʜ(PPE DPEF CVUPGGFOTF IBQQFOT w'BMTFOFHBUJWFʜ#BE DPEF CVUPGGFOTFEPFT OPUIBQQFO 3FUSPTQFDUJWFUFSNT

  136. 3FUSPTQFDUJWFUFSNT QPTJUJWF OFHBUJWF 
 USVF
 *UJTEFUFDUFEDPSSFDUMZ *UJTOPUEFUFDUFEDPSSFDUMZ 
 GBMTF
 *UJTEFUFDUFEJODPSSFDUMZ

    *UJTOPUEFUFDUFEJODPSSFDUMZ
  137. GBMTFQPTJUJWF

  138. GBMTFQPTJUJWF

  139. GBMTFQPTJUJWF

  140. GBMTFQPTJUJWF

  141. GBMTFQPTJUJWF

  142. GBMTFQPTJUJWF

  143. *ODMVEFNBUDIFS

  144. 1SPQPTFlGBMTFOFHBUJWFz

  145. 1SPQPTFlGBMTFOFHBUJWFz

  146. 1SPQPTFlGBMTFOFHBUJWFz

  147. GBMTFQPTJUJWF ˣ GBMTFOFHBUJWF

  148. GBMTFQPTJUJWF

  149. FYQFDU@OP@PGGFOTFT

  150. FYQFDU@PGGFOTF

  151. 3FTPMWFEDPEF

  152. # In this PR, the following setting has been removed

    Rails/TopLevelInclude: Include: - app/**/*.rb $BVTFPGUIFQSPCMFN "EEBOFOUSZJOUPDPOpHEFGBVMUZNM *GEFGBVMUTFUUJOHJTSFRVJSFE *FYQBOEFEUIFTDPQFGSPN3BJMT UP4UZMFCVUUIFJNQMFNFOUBUJPO SFNBJOFEOBSSPX
  153. $VSSFOUMZUIFSFBSFOPGBMTFQPTJUJWFT

  154. w3FEVDFGBMTFQPTJUJWFTCZ SVOOJOHJO()SBJMTSBJMTSFQP w"OE3VOJOMPDBM3BJMTBQQ SFQP w*UNBZCFHPPEUPHPXJUI ()EJTDPVSTFEJTDPVSTF "DUJPOGSPN3FUSPTQFDUJWF

  155. "HFOEB 0OUIFEJGGFSFODF CFUXFFODPEJOHTUZMFBOE -JOU &NVMBUF3VCZ`TXBSOJOH  /FXDPQTUBSUTXJUI
 ZPVSQSPKFDU $SPTTPWFS
 044DPNNVOJUJFT

    1BSU 1BSU 1BSU
  156. 1BSU

  157. $SPTTPWFS 044 DPNNVOJUJFT

  158. "TUPSZBCPVU JNQPSUDVTUPNDPQ GSPN3BJMTUP3VCP$PQ VQTUSFBN ()SVCPDPQIRSVCPDPQ ()SBJMTSBJMT

  159. SBJMTSBJMT

  160. SVCPDPQZNMBUSBJMTSBJMT

  161. SVCPDPQZNMBUSBJMTSBJMT %JTBCMFE#Z%FGBVMUUSVF

  162. SBJMTSBJMT "EEDVTUPN3VCP$PQGPSABTTFSU@OPUAPWFSASFGVUFA

  163. None
  164. None
  165. 3BJMTDPNNJUUFST TVHHFTUFEUPNF !ZZBHJ !LBNJQP

  166. #FOFpUTPG3BJMT VQTUSFBN ()SVCPDPQIRSVCPDPQ ()SBJMTSBJMT w3FEVDFEFQFOEFODZPO3BJMT w5IJTDVTUPNDPQNBZCF VTFEFOEVTFST

  167. #FOFpUTPGFOEVTFST VQTUSFBN ()SVCPDPQIRSVCPDPQ :PVSQSPKFDU ()SBJMTSBJMT w3FEVDFEFQFOEFODZPO3BJMT w5IJTDVTUPNDPQNBZCF VTFEBMTPCZFOEVTFST

  168. w(FNJGZUPOFX 3VCP$PQ.5HFN  wABTTFSU@OPUANFUIPEJT B"45FTU$BTFNFUIPE *UJTO`UB.5NFUIPE 8IFSFUPFYQPSU SEQBSUZHFNT

  169. w(FNJGZUPOFX 3VCP$PQ.5HFN  wABTTFSU@OPUANFUIPEJT B"45FTU$BTFNFUIPE *UJTO`UB.5NFUIPE 8IFSFUPFYQPSU

  170. "45FTU$BTF.55FTU IUUQTHJUIVCDPNSBJMTSBJMTCMPCWBDUJWFTVQQPSUMJCBDUJWF@TVQQPSUUFTU@DBTFSC--

  171. w4P &YQPSUUIJTDPQ UP3BJMTEFQBSUNFOU JO3VCP$PQ 8IFSFUPFYQPSU

  172. SVCPDPQIRSVCPDPQ "EEOFXA3BJMT3FGVUF.FUIPETADPQ

  173. None
  174. None
  175. SVCPDPQIRSVCPDPQ SBJMTSBJMT

  176. TVQFS 3VCP$PQ$PQ $VTUPN$PQT 3FGVUF/PU PSJHJOBM DPQDMBTT 1MVHHBCMFBSDIJUFDUVSF

  177. TVQFS 3VCP$PQ$PQ $VTUPN$PQT 3FGVUF/PU 3BJMT3FGVUF.FUIPET .PTUMZUIFJNQMFNFOUBUJPOXBTJOUBDU PSJHJOBM DPQDMBTT JNQPSUFE DPQDMBTT

    1MVHHBCMFBSDIJUFDUVSF
  178. w3BJMTDPNNVOJUZ DPOUSJCVUPSDPNFUP 3VCP$PQDPNNVOJUZ "OJOUFSFTUJOHQIFOPNFOPO

  179. $PNNFOUFECZ"3BJMTDPOUSJCVUPS

  180. *NQPSUFEBOPUIFSDPQGSPN3BJMT

  181. 3VCP$PQ JT
 SFMFBTFE 3VCP$PQ JT
 FOBCMFEXJUI$PEF$MJNBUF *OUIFGVUVSFXPSME *GUIFGPMMPXJOHDPOEJUJPOT BSFNFU 3BJMTDBOMFBWF

    UIFTFDPQTUP3VCP$PQ
  182. #FUUFS MFBWFJ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UPSDVTUPNJ[FJU w:PVDBOPQFOB13UPVQTUSFBN BOZUJNF w5IFSFBSFWBSJPVTTUBSUJOHNFUIPET w6TFUIFPQQPSUVOJUZUPMJTUFOUP QFPQMFZPVLOPX %FGBVMUTUZMFJTO`UUIFPOMZTFUUJOH

  192. IUUQBHJMFNBOJGFTUPPSH

  193. IUUQBHJMFNBOJGFTUPPSH

  194. 4P :PVBMSFBEZ IBWF PQQPSUVOJUJFT

  195. None
  196. One more thing…

  197. 3BJMT0441BUDINFFUVQ

  198. 4PDJBMDPEJOHTUBSUTXJUIZPV :PV

  199. !LPJD 5IBOLT .*/"48"/

  200. l "QSPCMFNJTpYFE OPUCFDBVTFUIFSFBSF QFPQMFJOUSPVCMF  CVUCFDBVTFUIFSFBSF QFPQMFXIPXBOUUPpYJU ࠔ͍ͬͯΔਓ͕͍Δ໰୊͕௚ΔͷͰ͸ͳ͘ɺ ௚͢ؾͷ͋Δਓ͕͍Δ໰୊͕௚ΔCZ!LBNJQP