by dorianmariefr · 5y ago · 0 comments
class Stuff
attr_reader :list
def initialize
@list = [1, 2, 3, 4]
end
end
Then: Stuff.new.list.each { |item| ... }
Stuff.new.list.map { |item| ... }
Stuff.new.list.select { |item| ... }
And if you want top-level each/map/select methods, you could do delegate :each, :map, :select, to: :list class Stuff
include Enumerable
def initialize
@list = [1, 2, 3, 4]
end
def each
@list.each{ yield _1 }
end
end class Stuff(list):
pass
kinda makes the article pointless. List obviously only serves as an easy to understand example, not something you are actually trying to implement.I’m genuinely confused as to where you picked this up. Enumerator and Enumerable are endemic.
I think for loops are mostly used in Ruby by people that picked it up in other languages first and simply never unlearned it.