Skip to content

Instantly share code, notes, and snippets.

@marioaquino
Forked from coffeencoke/group_of_thingies.rb
Created December 1, 2011 03:22
Show Gist options
  • Save marioaquino/1413246 to your computer and use it in GitHub Desktop.
Save marioaquino/1413246 to your computer and use it in GitHub Desktop.
Test a block in Ruby
class GroupOfThingies
attr_accessor :thingies
# Use like this:
#
# group_of_thingies = GroupOfThingies.new
# group_of_thingies.each do |thing|
# puts "Check out this awesome thing: #{thing}!"
# end
#
# or you can exclude a thing
#
# group_of_thingies = GroupOfThingies.new
# group_of_thingies.each(:except => bad_thing) do |thing|
# puts "This is a good thing: #{thing}!"
# end
#
def each(options={})
thingies.each do |thing|
yield(thing) unless options[:except] == thing
end
end
end
describe GroupOfThingies do
subject { described_class.new }
context 'when an except case occurs' do
before do
subject.thingies = [:a]
end
it 'does not yield' do
lambda { subject.each(:except => :a) {|a| raise } }.should_not raise_exception
end
end
end
@adkron
Copy link

adkron commented Dec 5, 2011

This is a great solution. Simple and easy to follow. I can look at the spec and know what the code does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment