The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Little heplers for Ruby print debugging

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
Little heplers for Ruby print debugging Posted: Sep 4, 2010 11:39 AM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

Not everyone likes debuggers. I rather print the debug values myself – it works and I do not need to learn a debugger :P

The Zucker debug package includes various print debugging helpers.

q

Like p, but outputs on a single line, values separated by spaces.

Listing 1
/36/qq.rb ruby
### usage
# q "zucker", '', {6=>7}, 5, 2.3, :o
### will output
# "zucker"  ""  {6=>7}  5  2.3  :o

def q(*args)
  puts args.map( &:inspect )*'  ' unless args.empty?
end
alias qq q

o

Outputs the current line, the current file, the current method and a label (if given).

Listing 2
/36/oo.rb ruby
### 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.rb

def 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 #{$`}"
end
alias oo o

c

Displays the method call stack.

Listing 3
/36/cc.rb ruby 1.9
### usage
# def qwe
#  c
# end
#
# def rtz
#   qwe
# end
#
# rtz
#
### example output
# <main>
#   rtz
#     qwe


def c
  puts caller.reverse.map.with_index{ |m, i|
    m.rindex( /:\d+(:in `(.*)')?$/ )
    "  "*i + $2
  }
end
alias cc c

# The version in Zucker also filters irb methods

Object#d

Outputs to stdout and returns self.

# example usage
require '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.0834784520844

name = 'Earth'
'Hello ' + name.d{|e| "The length is: #{e.size}"}
# outputs "The length is: 5"
# => 'Hello Earth'

Binding#inspect

Displays your current variables.

# irb example
>> require 'zucker/debug'
=> true
>> a=3
=> 3
>> binding
=> #<Binding:0x94c4b50>
local vars
 - a: 3
 - _: #<Binding:0x94c4b50>
(instance vars)
 - none
self
 - main
block_given?
 - false

Object#m

An ordered method list.

A little detail

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 ;).

CC-BY (DE)

Read: Little heplers for Ruby print debugging

Topic: Amethyst - One Week Later Previous Topic   Next Topic Topic: Programming Challenge for Newbies in Clojure and Python too?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use