do/end blockでrescueが 使えるようになった #megurorb

do/end blockでrescueが 使えるようになった #megurorb

Meguro.rb#10 ここに来ればRuby2.5.0のすべてが分かる! クリスマス前スペシャル

https://megurorb.connpass.com/event/74033/

0f9840a3c048e0f0adc2f9411bca91dd?s=128

Yoshinori Kawasaki

December 19, 2017
Tweet

Transcript

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

    Yoshinori Kawasaki
  2. 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
  3. 3 ΂ΜΓʂ

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

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

  6. 6 ͓·͚

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

  8. 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
  9. 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
  10. 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
  11. 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
  12. 12 ࣗࣾͷ࠷େrailsϨϙδτϦͰ࣮ߦͯ͠Έͨ

  13. 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
  14. 14 ҙ֎ͱগͳ͔ͬͨ

  15. 15 ͓ΘΓ