Guest User

Untitled

a guest
Sep 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. (1..10).each {println(it)}
  2.  
  3. (1..10).each(&method(:puts))
  4.  
  5. module Enumerable
  6. def each_ie(&block)
  7. return to_enum(__method__) { each.size } unless block_given?
  8. each { |e| e.instance_eval(&block) }
  9. end
  10. end
  11.  
  12. (1..10).each_ie { puts self }
  13. # 1
  14. # 2
  15. # 3
  16. # 4
  17. # 5
  18. # 6
  19. # 7
  20. # 8
  21. # 9
  22. # 10
  23. #=> 1..10
  24.  
  25. class Enumerator
  26. def with_ie(&block)
  27. return to_enum(__method__) { each.size } unless block_given?
  28. each { |e| e.instance_eval(&block) }
  29. end
  30. end
  31.  
  32. (1..10).map.with_ie { self * self }
  33. #=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  34. (-5..5).select.with_ie { positive? }
  35. #=> [1, 2, 3, 4, 5]
  36.  
  37. people.map.with_ie { "#{id}: #{first_name} - #{last_name}" }
  38.  
  39. {a: 1, b: 2}.map.with_ie { self }
  40. #=> [[:a, 1], [:b, 2]]
  41. {a: 1, b: 2}.map.with_ie { self[0] }
  42. #=> [:a, :b]
Add Comment
Please, Sign In to add comment