Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Object
- def when(*a, &block)
- a.inject(true) {|m, n| m && n} ? block.call(*([self] + a)) : self
- end
- end
- upper_limit = nil
- puts [1, 2, 3, 4, 5].when(upper_limit) {|array, upper_limit| array.select {|e| e < upper_limit}}.map {|n| n ** 2}.inject(0) {|m, n| m + n}
- # => 55
- # OR
- upper_limit = 3
- puts [1, 2, 3, 4, 5].when(upper_limit) {|array, upper_limit| array.select {|e| e < upper_limit}}.map {|n| n ** 2}.inject(0) {|m, n| m + n}
- # => 5
Add Comment
Please, Sign In to add comment