Skip to content

Instantly share code, notes, and snippets.

@kana
Created November 20, 2012 10:47
Show Gist options
  • Save kana/4117228 to your computer and use it in GitHub Desktop.
Save kana/4117228 to your computer and use it in GitHub Desktop.
Trace message problem
flavors.each do |f|
begin
trace " #{f.repo_name} #{f.locked_version} ..."
f.deploy(vimfiles_path)
trace " done\n"
rescue
trace " failed\n"
raise
end
end
module Enumerable
def before_each(&tracer)
vs = each
Enumerator.new do |y|
loop do
v = vs.next
tracer.call(v)
y << v
end
end
end
def after_each(&tracer)
vs = each
Enumerator.new do |y|
loop do
v = vs.next
y << v
tracer.call(v)
end
end
end
def on_failure(&tracer)
vs = each
Enumerator.new do |y|
v = nil
begin
loop do
v = vs.next
y << v
v = nil
end
rescue StopIteration
raise
rescue
tracer.call(v)
raise
end
end
end
end
xs = Object.new
def xs.each
yield 1
yield 2
raise RuntimeError, 'bar!'
yield 3
end
# xs.to_enum.
# [1, 2, 3].to_enum.
[1, 2, 3].
before_each {|x| puts "before #{x}"}.
after_each {|x| puts "success #{x}"}.
on_failure {|x| puts "failure #{x}"}.
each do |x|
puts "doing #{x}"
raise RuntimeError, 'foo!' if x == 3
puts "done #{x}"
end
YUKI.N> ruby t.rb
before 1
doing 1
done 1
success 1
before 2
doing 2
done 2
success 2
before 3
doing 3
failure 3
t.rb:53:in `block in <main>': foo! (RuntimeError)
from t.rb:28:in `<<'
from t.rb:28:in `block (2 levels) in trace_failure_each'
from t.rb:24:in `loop'
from t.rb:24:in `block in trace_failure_each'
from t.rb:47:in `each'
from t.rb:47:in `each'
from t.rb:47:in `<main>'
module EnumerableExtension
def before_each(&tracer)
vs = each
Enumerator.new do |y|
loop do
v = vs.next
tracer.call(v)
y << v
end
end
end
def after_each(&tracer)
vs = each
Enumerator.new do |y|
loop do
v = vs.next
y << v
tracer.call(v)
end
end
end
def on_failure(&tracer)
vs = each
Enumerator.new do |y|
v = nil
begin
loop do
v = vs.next
y << v
v = nil
end
rescue StopIteration
raise
rescue
tracer.call(v)
raise
end
end
end
end
module Enumerable
include EnumerableExtension
end
xs = Object.new
def xs.each
yield 1
yield 2
raise RuntimeError, 'bar!'
yield 3
end
# xs.to_enum.
# [1, 2, 3].to_enum.
[1, 2, 3].
before_each {|x| puts "before #{x}"}.
after_each {|x| puts "success #{x}"}.
on_failure {|x| puts "failure #{x}"}.
each do |x|
puts "doing #{x}"
raise RuntimeError, 'foo!' if x == 3
puts "done #{x}"
end
YUKI.N> ruby t.rb
t.rb:58:in `<main>': undefined method `before_each' for [1, 2, 3]:Array (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment