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

Rubyで書かれた ソースコードを読む技術 in kaigi on rails/technic-of-reading-source-code-written-in-ruby-for-kaigi-on-rails-2020

fukajun
October 03, 2020
7.3k

Rubyで書かれた ソースコードを読む技術 in kaigi on rails/technic-of-reading-source-code-written-in-ruby-for-kaigi-on-rails-2020

fukajun

October 03, 2020
Tweet

Transcript

  1. 自己紹介 - @fukajun - 株式会社ロコガイドで働くサーバーサイドエンジニア - データ基盤、インフラ周りを担当 - ときどきRuby&Railsでの開発に参加 -

    Railsとの出会い - Arelが入る前、Rails 2.3 or 3 くらい - sendagaya.rb主催 with @tkawa - 毎週月曜日に勉強会を開催 - そろそろ350回を超える!? - キーボード - デフォルト => hhkb type-s => NiZ atom66 (沼回避)
  2. コードを探すには?(メソッド編) Method#source_location 1 class Hoge 2 def run 3 end

    4 end hoge = Hoge.new > hoge.method(:run).source_location => ["hoge.rb", 2] > hoge.method(:run) # Ruby2.7〜 => #<Method: Hoge#run() hoge.rb:2>
  3. 具体的な動作について知るには? デバッガーをつかう - binding.irb (ruby標準) - binding.pry (pry gem) -

    byebug (byebug gem) From: .../photos/app/controllers/albums_controller.rb @ line 3 : 1: class AlbumsController < ApplicationController 2: def index => 3: binding.irb 4: end 5: end
  4. 具体的な動作について知るには? From: .../photos/app/controllers/albums_controller.rb @ line 3 : 1: class AlbumsController

    < ApplicationController 2: def index => 3: binding.irb 4: end 5: end irb(#<AlbumsController:0x00007f8d94ae4998>):001:0> params => <ActionController::Parameters {"controller"=>"albums", "action"=>"index"} permitted: false> ... irb(#<AlbumsController:0x00007f8d94ae4998>):007:0> controller_name => "albums" irb(#<AlbumsController:0x00007f8d94ae4998>):008:0> action_name => "index"
  5. 呼び出し元について知るには? .../app/controllers/albums_controller.rb:4:in `index' .../actionpack-6.0.3.3/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action' .../actionpack-6.0.3.3/lib/abstract_controller/base.rb:195:in `process_action' .../actionpack-6.0.3.3/lib/action_controller/metal/rendering.rb:30:in `process_action' .../actionpack-6.0.3.3/lib/abstract_controller/callbacks.rb:42:in

    `block in process_action' .../activesupport-6.0.3.3/lib/active_support/callbacks.rb:135:in `run_callbacks' : 省略 : .../rack-proxy-0.6.5/lib/rack/proxy.rb:57:in `call' .../railties-6.0.3.3/lib/rails/engine.rb:527:in `call' .../puma-4.3.6/lib/puma/server.rb:713:in `handle_request' .../puma-4.3.6/lib/puma/server.rb:472:in `process_client' .../puma-4.3.6/lib/puma/server.rb:328:in `block in run' .../puma-4.3.6/lib/puma/thread_pool.rb:134:in `block in spawn_thread' ※byebug起動中にスタックトレースをたどるときは、 up/down (スタック階層を上下移動) frame n (スタック階層n番へ移動), list (コード表示), edit (エディタで開く)を利用すると便利です
  6. 変更したgemを元に戻すには? bundle or gem pristine コマンドを使う - Gemfileにあるgemを全部 - bundle

    pristine - Gemfileにあるgem(extensionなしのみ)を全部 : 速い ※ - bundle exec gem pristine --all --no-extension - 変更したgemを覚えている場合 ※ - bundle exec gem pristine [GEMNAME] --no-extension ※extensionありではうまく動作しなかった