Created
June 18, 2012 01:07
-
-
Save jessieay/2946260 to your computer and use it in GitHub Desktop.
Practicing procs, still not sure why this works
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 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