Last active
December 19, 2015 01:39
-
-
Save rubydubee/5877400 to your computer and use it in GitHub Desktop.
Conditional code inside inject could be fatal!
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
[32] pry(main)> words = %w{mary had a little lamb} | |
=> ["mary", "had", "a", "little", "lamb"] | |
[33] pry(main)> total = words.inject(0){ |result, word| word.size + result} | |
=> 18 | |
[34] pry(main)> total = words.inject(0){ |result, word| word.size + result if word.size > 3} | |
TypeError: nil can't be coerced into Fixnum | |
from (pry):52:in `+' | |
[35] pry(main)> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment