The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Enhanced xmp code evaluation and annotation

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
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Enhanced xmp code evaluation and annotation Posted: Nov 6, 2005 5:46 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Enhanced xmp code evaluation and annotation
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

Motivation

When I was writing Changes in Ruby 1.9, I had to evaluate lots of small snippets under ruby 1.8 and 1.9, both to illustrate the differences and to verify if some particular change described in the changelogs still applied --- many were reverted later. I wanted to do it all from vim and hence looked into gotoken's xmp and the derived version found in rubygarden. The latter would fail for some examples, forcing me to suspend vim, evaluate the snippet with ruby and ruby19, and finally paste the results... Way too much work.

So I wrote a new xmp-style filter, hopefully more robust than the original one, and more featureful. This is what you get:

  • you can indicate which lines have to be annotated with the result value
  • the values for multiple runs of a given line are aggregated
  • new annotations corresponding to the warnings Ruby issued during parsing or execution
  • if an exception is raised, it will be collected and displayed
  • the stdout output is also collected

Example

This filter will turn

# specify which values you want with # =>
RUBY_VERSION # => 
a = @a
class Foo
  def baz(n)
    (1..n).inject do |s,x|
      s + x   # =>
    end
  end

  def bar(x)
    x.gsub(/foo/, "bar") # =>
  end
  
  1+1 # =>
end

a = Foo.new
a.bar("this is a foo") # =>
b = a.bar("this foo is foo") # =>
a.baz(4)
puts b
Foo.new.bar

into

# specify which values you want with # =>
RUBY_VERSION # => "1.8.3"
a = @a # !> instance variable @a not initialized
class Foo
  def baz(n)
    (1..n).inject do |s,x|
      s + x   # => 3, 6, 10
    end
  end

  def bar(x)
    x.gsub(/foo/, "bar") # => "this is a bar", "this bar is bar"
  end
  
  1+1 # => 2
end

a = Foo.new
a.bar("this is a foo") # => "this is a bar"
b = a.bar("this foo is foo") # => "this bar is bar"
a.baz(4)
puts b
Foo.new.bar
# ~> -:23:in `bar': wrong number of arguments (0 for 1) (ArgumentError)
# ~> 	from -:23
# >> "this bar is bar\n"

Needless to say, this is quite useful for ruby-talk postings, for instance. Or for cheap and dirty debugging/testing, to verify what is going on inside a tight loop for instance. It takes but a keypress given the right keyboard mappings (see below).

The code

On to the code:


Read more...

Read: Enhanced xmp code evaluation and annotation

Topic: Turning Hiki into a simple CMS Previous Topic   Next Topic Topic: Haskell bundle for TextMate

Sponsored Links



Google
  Web Artima.com   

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