Skip to content

Instantly share code, notes, and snippets.

@rubydubee
Last active December 19, 2015 01:39
Show Gist options
  • Save rubydubee/5877400 to your computer and use it in GitHub Desktop.
Save rubydubee/5877400 to your computer and use it in GitHub Desktop.
Conditional code inside inject could be fatal!
[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