Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- # Sum the values of an array of arrays and numerics
- #
- # Sample Usage:
- # > arr = [1, 5 ,3 , [[], 4 , 1, 9, 0], 1]
- # > mixed_type_sum(arr)
- # => 24
- def mixed_type_sum(obj)
- return obj.inject(0){ | total, x |
- total += (x.respond_to? :each) ? mixed_type_sum(x) : x
- }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement