Skip to content

Instantly share code, notes, and snippets.

@onethirtyfive
Created August 18, 2011 00:26
Show Gist options
  • Save onethirtyfive/1153003 to your computer and use it in GitHub Desktop.
Save onethirtyfive/1153003 to your computer and use it in GitHub Desktop.
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