ããã«ã¡ã¯ãæè¡é¨ã®æ·±è°·(@fukajun)ã§ãã
å
æ¥(2020å¹´10æ3æ¥)è¡ãããKaigi on Railsã«ã¦ããRubyã§æ¸ãããã½ã¼ã¹ã³ã¼ããèªãæè¡ãã¨ããã¿ã¤ãã«ã§çºè¡¨ãã¾ããã
çºè¡¨ããå 容ã«ã¤ãã¦
ä»äºããã©ã¤ãã¼ãã§ã¯ã¡ã¤ã³ã®Rubyãå«ãã¦ããã¤ãã®è¨èªã«ãµãã¦ãã¾ãããRubyã§æ¸ãããã³ã¼ãã¯é常ã«èªã¿ãããã¨æ¥ã
æãã¦ãã¾ãã
åºæ¬çã«ã¯ãRuby/Railsã®ã³ã¼ãã¯è¤éãªãã¸ãã¯ã§ããããªãç解ã§ãããã¨ãå¤ãã§ãã
ã¨ã¯ããããªããªãåããããããªãã³ã¼ãã«åºä¼ããã¨ãããã¾ãã
ä»åã¯ããããã£ãå ´åã«èªåããã£ã¦ãããã¨ãä¸å¿ã«ç´¹ä»ãã¾ããã
çºè¡¨è³æ
å½æ¥ã使ç¨ããçºè¡¨è³æã§ãã
çºè¡¨åç»
Rubyã§æ¸ãããã½ã¼ã¹ã³ã¼ããèªãæè¡ / fukajun
çºè¡¨ã«å«ãããã¨ãã§ããªãã£ãå 容ã«ã¤ãã¦
ããããã¯ãæéã®é½åã§æ¬ç·¨ã«çãè¾¼ããªãã£ãå 容ãç´¹ä»ãã¾ãã
gemã®installããã¦ãããã£ã¬ã¯ããªãä¸çºã§éã
1ã¤ç®ã¯ãç¹å®ã®gemã®ã½ã¼ã¹ã³ã¼ããèªãã¨ãã«ä¾¿å©ãªæ¹æ³ã«ã¤ãã¦ã§ãã
çºè¡¨ã§ã¯ããé¢é£ããgemã®ã½ã¼ã¹ã³ã¼ããæ¤ç´¢ããããããæ¹æ³ãããã¡ã½ããã®å®ç¾©å ´æãæ¢ãæ¹æ³ãã«ã¤ãã¦è©±ãã¾ãããããç¹å®ã®gemã®ãã£ã¬ã¯ããªãéãæ¹æ³ãã«ã¤ãã¦ã¯è§¦ãã¾ããã§ããã
ããã«ã¤ãã¦ã¯bundler
ã¾ãã¯gem
ã®open
ã³ãã³ããå©ç¨ãããã¨ã§ãgemãã¤ã³ã¹ãã¼ã«ããã¦ãããã£ã¬ã¯ããªãã¨ãã£ã¿ã¼ã§ãµãã¨éãã¾ãã
以ä¸ã®ä¾ã§ã¯ãactivesupport
gemã®ãã£ã¬ã¯ããªãéãã¦ãã¾ãã
$ bundle open activesupport
åã«ãã£ã¬ã¯ããªãã¹ã表示ããæ¹æ³ãããã¾ãã
$ bundle info activesupport > * activesupport (6.0.3.3) > Summary: A toolkit of support libraries and Ruby core extensions extracted from the Rails framework. > Homepage: https://rubyonrails.org > Path: /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activesupport-6.0.3.3
ã¡ãªã¿ã«ã以åãã®ç®çã§å©ç¨ãã¦ããbundle show
ã¯DEPRECATEDã«ãªã£ãããã§ãã
ç¹°ãè¿ãå®è¡ãããã³ã¼ãã®ç¹å®ã®å®è¡ç¶æ ãç¥ããã
2ã¤ç®ã¯ãç¹°ãè¿ãå®è¡ãããã³ã¼ãã®ç¹å®ã®å®è¡ç¶æ
ãã¿ãæ¹æ³ã«ã¤ãã¦ã§ãã
çºè¡¨ã§ã¯ãå
·ä½çãªåä½ãç¥ãããã«byebug
,binding.irb
ãªã©ã®ãããã¬ãå©ç¨ãã話ããã¾ããã
åããã¦ã¿ã¦1,2åå®è¡ãããã³ã¼ãã§ããã°æ±ããããã§ããã
ç¹°ãè¿ãå®è¡ãããã³ã¼ãã§ã¯ããã®è¡ã§ä½åº¦ãæ¢ã¾ã£ã¦ãã¾ãç¹å®ã®å®è¡ç¶æ
ã¾ã§é²ããã®ã¯å¤§å¤ã§ãã
ãããªã¨ãã¯ãé常ã«ã·ã³ãã«ã§ããif/unless
ã¨çµã¿åããã¦ç¹å®ã®æ¡ä»¶ã®ã¿ãããã¬ãèµ·åããããã«ããã¨èªåãç¥ãããå®è¡ç¶æ
ãã¿ããã¨ãã§ãã¾ãã
100.times do |i| byebug # ä½åãæ¢ã¾ã£ã¦ãã¾ãã®ã§ããããã®ãå¤§å¤ end # é©ç¨å¾ 100.times do |i| byebug if i == 42 end
便å©ã«ã¹ã¿ãã¯ãã¬ã¼ã¹ããã©ã
3ã¤ç®ã¯ãããããã楽ã«ã¹ã¿ãã¯ãã¬ã¼ã¹ã«ããã³ã¼ãããã©ãæ¹æ³ã«ã¤ãã¦ã§ãã
çºè¡¨ã®ãªãã§ãcaller
ãbacktrace
ã§ã¹ã¿ãã¯ãã¬ã¼ã¹ã表示ããã¨ãã£ã¿ã¼ã使ã£ã¦é ã«ãã©ã£ã¦ãã話ããã¾ããã
ãã®ã¨ãè³æã«æ¸ãã¦ãããbyebugãå©ç¨ãã¦ããã¨ãã®ããã«ä¾¿å©ãªèªã¿æ¹ãã«ã¯è§¦ããããªãã£ãã®ã§ãããã§ããå°ã詳ãã説æãããã¨æãã¾ãã
byebug
ãããã°ä¸ã«backtrace
ã³ãã³ããå®è¡ããã¨ã¹ã¿ãã¯ãã¬ã¼ã¹ããºã©ãã¨è¡¨ç¤ºããã¾ãã
(byebug) backtrace --> #0 AlbumsController.index at /Users/fukajun/reading-code/photos/app/controllers/albums_controller.rb:4 #1 ActionController::BasicImplicitRender.send_action(method#String, *args#Array) at /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.3/lib/action_controller/metal/basic_implicit_render.rb:6 #2 AbstractController::Base.process_action(method_name#String, *args#Array) at /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.3/lib/abstract_controller/base.rb:195 ... 以ä¸çç¥ ...
ãã®ã¹ã¿ãã¯ãã¬ã¼ã¹ã®é層ãup
ã³ãã³ããå©ç¨ããã¨byebug
ã§æ¢ãã¦ããä½ç½®ããé ã«ä¸ããã¨ãã§ãã¾ãã(éã«down
ã§ã¯ä¸ããã¨ãã§ãã¾ãï¼
ä¸è¨ã®ä¾ã§ã¯2åup
ãå®è¡ãã¦ã2é層ä¸ã£ã¦ãã¾ãã
åå®è¡çµæã«ã次ã®é層ã®å¼ã³ã ãè¡ãä¸å¿ã«ã½ã¼ã¹ã³ã¼ãã表示ããã¾ãã
ã¨ãã£ã¿ã¼ã§ã³ã¼ããéããã¨ãªããã¹ã¿ãã¯ãã¬ã¼ã¹ã追ããã®ã§ããªã楽ã«èªããããã«ãªãã¾ãã
ããå°ãã表示è¡æ°ãã»ããã¨ãã¯set listsize <表示ããè¡æ°>
ã§å¢ããã¾ãã
ããã«ã該å½ã®ãã¡ã¤ã«å
¨ä½ãè¦ãããªã£ãå ´åã¯editor
ã³ãã³ããå®è¡ã
vimãªã©ã®ã¨ãã£ã¿ã¼ã§è©²å½ãã¡ã¤ã«ãéããã¨ãã§ãã¾ãã
[1, 5] in /Users/fukajun/reading-code/photos/app/controllers/albums_controller.rb 1: class AlbumsController < ApplicationController 2: def index 3: byebug => 4: end 5: end (byebug) up [4, 8] in /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.3/lib/action_controller/metal/basic_implicit_render.rb 4: module BasicImplicitRender # :nodoc: 5: def send_action(method, *args) => 6: super.tap { default_render unless performed? } 7: end 8: (byebug) up [193, 197] in /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.3/lib/abstract_controller/base.rb 193: # which is *not* necessarily the same as the action name. 194: def process_action(method_name, *args) => 195: send_action(method_name, *args) 196: end 197: (byebug) editor # abstract_controller/base.rbã®195è¡ç®ãvimçã§éãã¦ããã¾ã
set listsize 20
ãå®è¡ã表示è¡æ°ãå¢ããã¦ã¿ã¾ããã
ä¸æ¹ã«ããsend_action
ãsend
ã®alias
ã¨ãã¦å®ç¾©ããã¦ããè¡ã¾ã§èªããããã«ãªãã¾ããã
(byebug) set listsize 20 [185, 204] in /Users/fukajun/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.3.3/lib/abstract_controller/base.rb 185: self.class.action_methods.include?(name) 186: end 187: 188: # Call the action. Override this in a subclass to modify the 189: # behavior around processing an action. This, and not #process, 190: # is the intended way to override action dispatching. 191: # 192: # Notice that the first argument is the method to be dispatched 193: # which is *not* necessarily the same as the action name. 194: def process_action(method_name, *args) => 195: send_action(method_name, *args) 196: end 197: 198: # Actually call the method associated with the action. Override 199: # this method if you wish to change how action methods are called, 200: # not to add additional behavior around it. For example, you would 201: # override #send_action if you want to inject arguments into the 202: # method. 203: alias send_action send 204:
åé層ã«ç§»åããã¨ãã¯ããã®ã¹ã³ã¼ãã®å¤æ°ãvar i
ãvar l
ã§ã¿ããã¨ãã§ãã¾ãã
ã¡ãªã¿ã«ã表示ãããå¤æ°ã®ç¶æ
ã¯ããã®é層ã®ã¨ãã®ç¶æ
ã§ã¯ãªããä»ç¾å¨ã«ãããå¤æ°ã®ç¶æ
ã§ãããã¨ãè¦ãã¦ããã¨æ··ä¹±ããªãã¦æ¸ã¿ããã§ãã
ãããã¬ã¼ãå©ç¨ãã¦ã³ã¼ããèªãã®ã¯å°ãæéã§ããã
ã¨ãã£ã¿ã¼ã§ä½åº¦ãéãç´ããã¨ãªãã³ã¼ãããã¬ãã¥ã¼ããªããã¹ã¿ãã¯ãã¬ã¼ã¹ã追ããã®ã§ããã°ãæ´»èºããå ´é¢ãå¢ããã®ã§ã¯ãªãã§ããããï¼
ã¾ã¨ã
ç»å£ããã£ããã«èªåããã¤ãã©ã®ããã«Rubyã®ã³ã¼ããèªãã§ãããæ¯ãè¿ããã¨ãã§ãã¾ããã
ã¾ããããããã便å©ãªæ¹æ³ã¯ãªãã調ã¹ããã£ããã«ããªã£ã¦ããã£ãã§ãã
ããã¨ãåå¦è
åãã®å
容ãã¨ãããã¨ã§ãåå è
ã®æ±ããå
容ã«ãªã£ã¦ãããã¯æ°ã«ãªãã¨ããã§ã¯ããã¾ããããã£ã¨èª°ãã®å½¹ã«ç«ã¦ãã®ã§ã¯ãªããã¨ä¿¡ãã¦ãã¾ãã
ä»å¾ããªã«ãæ©ä¼ãã¿ã¤ãã¦ãç»å£ã»ããã°ãªã©ç¶ç¶çãªã¢ã¦ããããããã¦ããããã¨æãã¾ãã
æå¾ã«ãªãã¾ãããç»å£è³æã®ã¬ãã¥ã¼ããã¦ãããå¼ç¤¾ã¨ã³ã¸ãã¢ã¡ã³ãã¼ã¨ãKaigi on Railsã¤ãã³ãã¹ã¿ããã®æ¹ã
ã«æè¬ãããã¨æãã¾ãã