Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (1..10).each {println(it)}
- (1..10).each(&method(:puts))
- module Enumerable
- def each_ie(&block)
- return to_enum(__method__) { each.size } unless block_given?
- each { |e| e.instance_eval(&block) }
- end
- end
- (1..10).each_ie { puts self }
- # 1
- # 2
- # 3
- # 4
- # 5
- # 6
- # 7
- # 8
- # 9
- # 10
- #=> 1..10
- class Enumerator
- def with_ie(&block)
- return to_enum(__method__) { each.size } unless block_given?
- each { |e| e.instance_eval(&block) }
- end
- end
- (1..10).map.with_ie { self * self }
- #=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
- (-5..5).select.with_ie { positive? }
- #=> [1, 2, 3, 4, 5]
- people.map.with_ie { "#{id}: #{first_name} - #{last_name}" }
- {a: 1, b: 2}.map.with_ie { self }
- #=> [[:a, 1], [:b, 2]]
- {a: 1, b: 2}.map.with_ie { self[0] }
- #=> [:a, :b]
Add Comment
Please, Sign In to add comment