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