Ruby 㧠debug ãã7ã¤ã®æ¹æ³
Perl ã§ã® print debug ã®æ¹æ³ã®ç´¹ä»ããã¼ã (?)ã ã£ãã®ã§ãèªåãããè¡ã£ã¦ã Ruby ã§ã® debug æ¹æ³7ã¤ã«ã¤ãã¦æ¸ãã¦ã¿ã¾ãã
p
ãåãã®äººãå¤ã Kernel#p ã¡ã½ãããããã使ãã¨ãªãã¸ã§ã¯ãã®å 容ãè¦ãããå½¢ã§åºåãã¦ããã¾ãã
>> p ({:foobar => :baz}) {:foobar=>:baz}
Object#inspect ã使ãã¨ãp ã§åºåããã¨ãã¨åãæååã String ã¨ãã¦åå¾ã§ãã¾ãã
>> puts ({:foobar => :baz}).inspect {:foobar=>:baz}
åå¿è ã®é ãã® p ã§ã®åºåã使ãæ¹æ³ãããããªãã¦å°ã£ãè¨æ¶ãâ¦ã
pp
pp ã¨ããã©ã¤ãã©ãªã使ãã¨ãp ãããããè¦ãããå½¢å¼ã§åºåãã¦ããã¾ãããã¨ãã°
>> a = Array.new(10) { {:foobar => :baz} } >> p a [{:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}]
ãªæãã§ãArray ã Hash ã®å 容ã大ãããªã£ã¦ããã¨ããããã«æãè¿ããããè¦ã¥ãããªã£ã¦ãã¾ãã¾ãããpp ã使ãã¨ããã解æ¶ã§ãã¾ãã
>> require 'pp' >> pp a [{:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}]
ã¾ã pp ã®åºåã String ã¨ãã¦åå¾ãããå ´å㯠PP.pp ã®ç¬¬ä¸å¼æ°ã« object ãã第äºå¼æ°ã«ç©ºæååã渡ããã¨ã§åå¾ã§ãã¾ãã
>> str = PP.pp(a,'') >> puts str [{:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}, {:foobar=>:baz}]
ãã㨠BK ãªåå¾æ¹æ³ã§ãããã½ã¼ã¹ãè¦ãæãã§ã¯ãã以å¤ã®æ¹æ³ãç¡ãæ°ããã¾ãã
追è¨
ruby >= 1.8.5 ãªæ·»ä»ã©ã¤ãã©ãªã® pp.rb ã§ã¯ Kernel#pretty_inspect ã追å ããã¦ããã
str = obj.pretty_inspect
ã§æååã¨ãã¦åå¾ã§ãã¾ããã³ã¡ã³ãã«ã¦æãã¦ãããã¾ããã
y
require 'yaml' ããã¨ãKernel#y ã¡ã½ããã追å ãããy obj 㧠YAML å½¢å¼ã§ Object ã®ä¸èº«ãè¦ããã¨ãã§ãã¾ãã
>> y ({:foobar => :baz}) --- :foobar: :baz
pp ã§ããããããã¨ããæ°ãããã®ã§å人çã«ã¯ã»ã¨ãã©ä½¿ã£ã¦ã¾ããããã¨ããã¾ html ä¸ã«ãªãã¸ã§ã¯ãã®ä¸èº«ã表示ããããæã«ä½¿ãã¨ããããã¾ãããªããªã p/pp ã§ã¯ Object#inspect ã¾ãã¾ã®è¡¨ç¤ºãªã®ã§ã
>> p Object.new #<Object:0x409095bc>
ã¨ã¿ã°ã§å²ã¾ãã¦åºåããããããHTMLã¨ã¹ã±ã¼ããå¿ è¦ãªã®ã§ãããYAMLã®å ´åã¯
>> y Object.new --- !ruby/object {}
ã¨å¿ è¦ãªãããã§ãã
backtrace ã追ã
ãã¾ã¾ã§ããã°ã©ã ãã©ããã£ã¦å®è¡ããã¦ããã®ãã追ãã«ã¯ backtrace ãè¦ãã®ãä¸çªã§ãã以å㯠Exception ãçºçããã¦ãã® backtrace ãè¦ããã¨ãããªãã¨ãå¾®å¦ãªãã¨ããã¦ãããã§ãããã¡ããã©ä¸å¹´ã»ã©å id:ha-tan:20051014:1129218253 ã§æãã¦ããããcaller ãè¦ãã°ãããã¨ããããã¾ããã
ãªã®ã§
class Foo def foo bar end def bar baz end def baz p caller end end Foo.new.foo
ãªå¼ã³åºãã¯
["call.rb:6:in `bar'", "call.rb:3:in `foo'", "call.rb:13"]
ã¨ãæåã«13è¡ç®ã§ã¡ã½ãããå¼ã³ã ããã次㫠foo ã¡ã½ããå é¨3è¡ç®ã§ã¡ã½ãããå¼ã³åºããããã®å¾ bar ã¡ã½ããå é¨ã®6è¡ç®ã§å¼ã³åºããâ¦ãã¨å®¹æã«è¿½ã£ããããã¨ãã§ãã¾ãã
set_trace_func
set_trace_func ã¨ããçµã¿è¾¼ã¿é¢æ°ã§ãã¡ã½ããå¼ã³åºããå¼ã®è©ä¾¡ãªã©ãæ§ã
ãªã¤ãã³ãã«ããã¯ãã¦æç¶ããªãã¸ã§ã¯ããå®è¡å¯è½ã§ãã
å²ã¨ä½ã§ãã§ããå¼·åãªãªãã¬ã¯ã·ã§ã³ã¨ãã¦ä½¿ãã¾ãããéããªãã®ã§ãããã°å°ç¨ãªæãã§ãã
ãã¨ãã° bar ã¨ããååã®ã¡ã½ãããå¼ã³åºãããæã«ã¢ã¯ã·ã§ã³ãèµ·ããããã¨ãã¯ãããªæãã«ã
class Foo def foo bar end def bar end end class Bar < Foo def bar end end set_trace_func lambda { |event, file, line ,id, binding, klass| if event == 'call' && id == :bar puts "call method bar file:#{file} line:#{line}" end } Foo.new.foo Bar.new.foo
çµæã¯
call method bar file:call.rb line:5 call method bar file:call.rb line:10
logger ã使ã
Class ãä½ãã¨ã㯠logger ã¤ã³ã¿ã¼ãã§ã¤ã¹ãä½ã£ã¦ããã¨ä½ãã¨ä¾¿å©ã§ããæè¿ã®å人çãªã¹ã¿ã³ãã¼ããªãã³ãã¬ã¯
require 'logger' class Foo def initialize(logger = nil) @logger = logger || Logger.new(STDERR) end attr_reader :logger def foo logger.debug 'call:foo' end end Foo.new.foo #=> D, [2006-10-10T12:33:16.581481 #10500] DEBUG -- : call:foo
ãªæã㧠initialize ã§ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã« logger ãå
¥ãã¦ãããattr_reader ã§èªã¿åãç¨ã®ã¢ã¯ã»ãµã« @logger ãè¨å®ãããã¨ã§ããã®ã¯ã©ã¹å
å´ããã¯ã©ãããã§ã logger ã§åç
§ã§ããããã«ãã¦ã¾ããRails è³ã
ã¾ããã®ã¯ã©ã¹ã Rails ããå¼ã³åºãã¦ä½¿ãæã¯
Foo.new RAILS_DEFAULT_LOGGER
㧠Rails æ¨æºã® logger ã¤ã³ã¹ã¿ã³ã¹ãå©ç¨ãã¦ãã°ãã¯ãã¦ä¾¿å©ã§ãã
profiler ã使ã
é度ãåé¡ã«ãªã£ã¦ããã profile.rb ã使ãã¾ããã©ã¤ãã©ãªåä½ãªã©ãªãã-rprofile ãä»ãã¦å®è¡ããã ã㧠OK ã§ãã
$ ruby -rprofile call.rb % cumulative self self total time seconds seconds calls ms/call ms/call name 66.67 0.02 0.02 84 0.24 0.24 Module#method_added 33.33 0.03 0.01 2 5.00 20.00 Kernel.require 0.00 0.03 0.00 1 0.00 0.00 Array#[] 0.00 0.03 0.00 4 0.00 0.00 Thread#critical= 0.00 0.03 0.00 2 0.00 0.00 Fixnum#* 0.00 0.03 0.00 1 0.00 0.00 MonitorMixin.mon_exit 以ä¸ç¥
ãããªæãã§åºåããã¾ããããã profile.rb ã¯é ãã®ã§å°ãã¨ããããã¾ããããããªã¨ãã«ã¯ ruby-prof ã使ãã¾ããããå©ç¨æ¹æ³ã¯ profile.rb ã¨ã»ã¨ãã©åãã§
$ ruby -runprof call.rb
ã¨ããã ãã§ãã
ãã ãã® Ruby ã¤ã³ã¿ããªã¿èµ·åæã« require ã®æ¹æ³ã§åé¡ã«ãªãã®ã¯ãå®è¡çµæãã¹ã¦ã§ãããã¡ã¤ãªã³ã°ãã¦ãã¾ãããããã¬ã¼ã ã¯ã¼ã¯ä¸ã®ä¸é¨åã ãããããã¡ã¤ãªã³ã°ãããã¨ããªã©ã«å°ãã¾ãããããªå ´åã¯ãRubyProf.profile ã¡ã½ããã«ãããã¯ã渡ãã° OK ã§ããã§ãçµæã Printer ã§åºåãã¾ãã
require 'rubygems' require 'ruby-prof' require 'stringio' # ãã®é¨åã ããããã¡ã¤ãªã³ã° result = RubyProf.profile do 100.times { Regexp.new 'foo' } end printer = RubyProf::FlatPrinter.new(result) strio = StringIO.new printer.print(strio) puts strio.string # çµæãæååã¨ãã¦åå¾
æ´å½¢ããæååã§åå¾ãããã¨ããé¢åãªã®ã§ããããªã©ã¤ãã©ãªãæ¸ãã¦ä½¿ã£ã¦ã¾ãã
require 'rubygems' require 'ruby-prof' require 'stringio' module RubyProf def profiler(&block) result = RubyProf.profile &block printer = RubyProf::FlatPrinter.new(result) strio = StringIO.new printer.print(strio) strio.string end module_function :profiler end
ããã require ããã¨
result = RubyProf.profiler { 100.times { Regexp.new 'foo' } } logger.debug result
ã ãã§æååã¨ãã¦çµæã¨ãã¦åå¾ã§ããã®ã§ãlogger ãªããªããªãã«åºåãã¾ãã
ãããªæãã§ãããã debug æ¹æ³ãç´¹ä»ãã¾ãããããã better ãªæ¹æ³ãªã©ãªã©ããã£ããã³ã¡ã³ããªããã©ãã¯ããã¯ãªãã§æãã¦ããããã¨ããããã§ãã