-
-
Save marioaquino/1413246 to your computer and use it in GitHub Desktop.
Test a block in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great solution. Simple and easy to follow. I can look at the spec and know what the code does.