From: Ryan Davis Date: 2012-09-08T06:19:04+09:00 Subject: Re: assert All lines On Sep 7, 2012, at 05:43 , Mario Ruiz wrote: > Hi, > I'm trying to create a method to assert all lines and if any of them > fail then the whole block will fail > require 'test/unit' > > def assert_all(message="assert_block failed.") # :yields: > _wrap_assertion do > if (!yield) > puts "ERROR" > raise Test::Unit::AssertionFailedError.new(message.to_s) > else > puts "PERFECT" > end > end > end > > class MyTest < Test::Unit::TestCase > def test01 > assert_all { > 1==1 > 2==3 > 3==3 > } > end > end Switch {} to () and add commas: def assert_all(message, *exprs) exprs.each_with_index do |expr, i| assert expr, "Failed on #{i+1}: #{message}" end end I know you're trying to make this as succinct and clean as possible, but there is no way to individually evaluate each expression w/o some seriously heinous "magic". This alternative tries to balance out the magic vs the syntax. I also removed the optional message argument for a mandatory one so you don't have to try to distinguish between a message and the first assertion. The alternative to that is to pass in a hash with expr => message, but that starts to get gross too.