Slide 1

Slide 1 text

do/end blockͰrescue͕ ࢖͑ΔΑ͏ʹͳͬͨ Meguro.rb#10 (1 min LT - ruby 2.5.0) Yoshinori Kawasaki

Slide 2

Slide 2 text

2 3ඵͰΘ͔Δղઆ ͜Ε·Ͱ (2.4) 0.times do begin 1 rescue => exception 2 else 3 ensure 4 end end ͜Ε͔Β (2.5) 0.times do 1 rescue => exception 2 else 3 ensure 4 end

Slide 3

Slide 3 text

3 ΂ΜΓʂ

Slide 4

Slide 4 text

4 Matz͸͋Μ·Γؾʹೖͬͯͳ͔ͬͨΒ͍͠ (ཁ๬͕ଟ͍ͷͰड͚ೖΕͨͱͷ͜ͱ) IUUQTCVHTSVCZMBOHPSHJTTVFT

Slide 5

Slide 5 text

5 ࣗવ͞Λॏࢹͯ͠{…}ͷblockʹద༻͠ͳ͔ͬͨ͋ͨΓ͕ ྑ͔ͬͨͬΆ͋͞Δ IUUQTCVHTSVCZMBOHPSHJTTVFT

Slide 6

Slide 6 text

6 ͓·͚

Slide 7

Slide 7 text

7 begin-rescue-else-ensure-end ΛΈ͚ͭΔϓϩάϥϜΛॻ͍ͨ

Slide 8

Slide 8 text

8 ෮श ͜Ε·Ͱ (2.4) 0.times do begin 1 rescue => exception 2 else 3 ensure 4 end end ͜Ε͔Β (2.5) 0.times do 1 rescue => exception 2 else 3 ensure 4 end

Slide 9

Slide 9 text

9 ripperͰparseͯ͠SࣜͰΈΔͱ ͜Ε·Ͱ (2.4) [:program, [[:method_add_block, [:call, [:@int, "0", [1, 0]], :".", [:@ident, "times", [1, 2]]], [:do_block, nil, [:bodystmt, [[:begin, [:bodystmt, [[:@int, "1", [3, 4]]], [:rescue, nil, [:var_field, [:@ident, "exception", [4, 12]]], [[:@int, "2", [5, 4]]], nil], [:else, [[:@int, "3", [7, 4]]]], [:ensure, [[:@int, "4", [9, 4]]]]]]], nil, nil, nil]]]]] ͜Ε͔Β (2.5) [:program, [[:method_add_block, [:call, [:@int, "0", [1, 0]], :".", [:@ident, "times", [1, 2]]], [:do_block, nil, [:bodystmt, [[:@int, "1", [2, 2]]], [:rescue, nil, [:var_field, [:@ident, "exception", [3, 10]]], [[:@int, "2", [4, 2]]], nil], [:else, [[:@int, "3", [6, 2]]]], [:ensure, [[:@int, "4", [8, 2]]]]]]]]] ※ripper͸rubyʹbundle͞Ε͍ͯΔެࣜparser

Slide 10

Slide 10 text

10 breee.rb (begin-rescue-else-ensure-end) def breee_body?(sexp) raise "Unexpected s-expression: #{sexp}" unless sexp[0] == :bodystmt sexp[1..-1].compact.any? do |sub_sexp| sub_sexp[0] == :rescue || sub_sexp[0] == :else || sub_sexp[0] == :ensure end end def find_breee_line(sexp) raise "Unexpected s-expression: #{sexp}" unless sexp[0] == :bodystmt (sexp.flatten.find { |e| e.is_a?(Integer) }) - 1 # heuristic end def find_breee(sexp) case sexp when nil, Symbol, String, Numeric, TrueClass, FalseClass [] when Array if sexp[0] == :do_block sub_sexp = sexp[2][1][0] if sub_sexp[0] == :begin && breee_body?(sub_sexp[1]) line = find_breee_line(sub_sexp[1]) end end [line, *sexp.map { |sub_sexp| find_breee(sub_sexp) }].compact.flatten else raise "Unexpected s-expression: #{sexp}" end end

Slide 11

Slide 11 text

11 breee.rb (contd.) file_names = Dir.glob('**/*.rb') file_names.each do |file_name| f = File.open(file_name) sexp = Ripper.sexp(f) line_nums = find_breee(sexp) line_nums.each do |line_num| puts "https://github.com/wantedly/wantedly/blob/master/#{file_name}#L#{line_num}" end end IUUQTHJUIVCDPNMVWUFDIOPCSFFFSVCZCMPCNBTUFSCSFFFSC

Slide 12

Slide 12 text

12 ࣗࣾͷ࠷େrailsϨϙδτϦͰ࣮ߦͯ͠Έͨ

Slide 13

Slide 13 text

13 ݁Ռ: 17݅ (LOC 109999 த) $ ruby ../../luvtechno/ruby-2.5.0/breee.rb https://github.com/wantedly/wantedly/blob/master/app/concerns/project_elasticsearch_concern.rb#L144 https://github.com/wantedly/wantedly/blob/master/app/concerns/social_profile_github_concern.rb#L31 https://github.com/wantedly/wantedly/blob/master/app/concerns/user_elasticsearch_concern.rb#L593 https://github.com/wantedly/wantedly/blob/master/app/concerns/user_elasticsearch_concern.rb#L713 https://github.com/wantedly/wantedly/blob/master/app/controllers/application_controller.rb#L296 https://github.com/wantedly/wantedly/blob/master/app/controllers/concerns/ notifications_controller_concern.rb#L9 https://github.com/wantedly/wantedly/blob/master/app/controllers/enterprise/scouts_controller.rb#L68 https://github.com/wantedly/wantedly/blob/master/app/models/company.rb#L734 https://github.com/wantedly/wantedly/blob/master/app/models/journal.rb#L212 https://github.com/wantedly/wantedly/blob/master/app/models/project.rb#L1065 https://github.com/wantedly/wantedly/blob/master/app/models/user.rb#L296 https://github.com/wantedly/wantedly/blob/master/lib/huntr/apply_standard_plan_options.rb#L13 https://github.com/wantedly/wantedly/blob/master/lib/huntr/date_and_time.rb#L64 https://github.com/wantedly/wantedly/blob/master/lib/huntr/db_circuit_breaker/circuit/base.rb#L114 https://github.com/wantedly/wantedly/blob/master/lib/huntr/treasure_data_to_big_query.rb#L6 https://github.com/wantedly/wantedly/blob/master/spec/models/project_tweet_spec.rb#L266 https://github.com/wantedly/wantedly/blob/master/spec/rails_helper.rb#L216

Slide 14

Slide 14 text

14 ҙ֎ͱগͳ͔ͬͨ

Slide 15

Slide 15 text

15 ͓ΘΓ