Created
August 18, 2011 00:26
-
-
Save onethirtyfive/1153003 to your computer and use it in GitHub Desktop.
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
letters = ['a', 'b', 'c', 'd'] | |
letters.each do |letter| | |
puts letter | |
end | |
=> prints each letter. easy. | |
# below, '' is an empty string. | |
# result_string is initially '', the argument passed to inject. | |
# that's how inject works. | |
letters.inject('') do |result_string, letter| | |
result_string << letter # concatenation, tack a letter onto the result_string | |
return result_string # after returning this here, the next iteration will receive _this_ value, not the initial | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment