This post originated from an RSS feed registered with Ruby Buzz
by Jan Lelis.
Original Post: Little heplers for Ruby print debugging
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;).
So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
### usage# o# o:Label### example outputs# Reached method `irb_binding', line 1 of file (irb)# Reached method `abc', line 4 of file 123.rb# Label: reached method `de', line 9 of file example.rbdef o(desc=nil)caller[0].rindex(/:(\d+)(:in (`.*'))?$/)m=$3?"method #$3, ":""d=desc?"#{desc}: r":'R'# lol, I need to change the syntax highlighter...# the "syntax" gem *crashed* when I wrote "#$`"puts"#{d}eached #{m}line #{$1} of file #{$`}"endaliasooo
# example usagerequire'zucker/debug'some.d.methods.d.noone.d.knows.d# ...outputs 4 lines with the inspected objects# => (result)21+Math.sin(42).d# outputs -0.916521547915634# => 20.0834784520844name='Earth''Hello '+name.d{|e|"The length is: #{e.size}"}# outputs "The length is: 5"# => 'Hello Earth'
You do not need to be afraid of having short variables names that might interfere with debug method names. As with p, Ruby does not care, if there is a local variable with that name, they just live side by side. Should a debug method be overwritten, you still have the double-character-variant (e.g. qq for q).
This is probably the last post on Zucker for a while. The development of Zucker went great last month. I am satisfied with the result and don’t think about pushing another version this month ;).