Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.30 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. # Sum the values of an array of arrays and numerics
  4. #
  5. # Sample Usage:
  6. # > arr = [1, 5 ,3 , [[], 4 , 1, 9, 0], 1]
  7. # > mixed_type_sum(arr)
  8. # => 24
  9.  
  10. def mixed_type_sum(obj)
  11.  
  12. return obj.inject(0){ | total, x |
  13.  
  14. total += (x.respond_to? :each) ? mixed_type_sum(x) : x
  15. }
  16.  
  17. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement