Ph values

Stephan on testing, software (good and bad), Ruby and the world at large

Archive for the ‘syntax’ Category

Syntax Highlight Code with TextMate in HTML

Posted by stephan on 10. March 2009

Andi Schnacke describes how to to generate HMTL to display syntax highlighted code from within TextMate. The short version:

Bundles β†’ TextMate β†’ Create HTML from Document

Strange I didn’t notice before.

Posted in Mac, Work Environment, syntax | Leave a Comment »

The Method you’re in

Posted by stephan on 28. January 2007

Sometimes I like to find out in which method my programs are currently in β€” may be for debugging but a lot more likely to log that fact for later analysis. For some (long running) automation stuff that’s really useful. Especially if (or when) the exact order of method calls it not deterministic. Think for example of a rules based engine which randomly applies a set of rules to a set of objects.

That said, you could just make each method log it:

def method_1
  logger.info "Executing #{ method_1 }"
  # more code
end

But that’s not DRY after the second method doing that. And it’s manual, cumbersome and error-prone.

Well, there’s Kernel#caller, but I was looking for something a bit more compact. So here it comes:

module MethodCallList
  def method_name
    list = caller[ 0..-2 ]
    if list.empty?
      "method_name"
    else
      list.map do | el |
        if md = el.match( /in '(.+)'/ )
          md[ 1 ]
        else
          ''
        end
      end.reverse.join( '/' )
    end
  end
end

Is there any better, more elegant or completely different way to do this in Ruby? I’d love to hear about that.
Hmm, the module name could be better than this…

Posted in Ruby, method call, syntax | 1 Comment »