Skip to content

Instantly share code, notes, and snippets.

@jessieay
Created June 18, 2012 01:07
Show Gist options
  • Save jessieay/2946260 to your computer and use it in GitHub Desktop.
Save jessieay/2946260 to your computer and use it in GitHub Desktop.
Practicing procs, still not sure why this works
class Array
def new_collect
self.collect do |n|
yield n
end
end
end
RSPEC:
describe Array do
describe "new_collect" do
it "collects the output of a block in an array" do
my_friends = ["Sally", "Mary", "Joe"]
expected_strings = ["I love Sally.", "I love Mary.", "I love Joe."]
my_strings = my_friends.new_collect { |friend| "I love " + friend + "." }
my_strings.should eq(expected_strings)
end
it "adds 1 to each element in an array" do
[1,2,3].new_collect { |a| a + 1 }.should eq([2,3,4])
end
it "maps everything to nil" do
[1,2,3].new_collect { nil }.should eq([nil, nil, nil])
end
it "maps classes" do
[1, 'cat', 1..1].map { |e| e.class }.should eq([Fixnum, String, Range])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment