Skip to content

Instantly share code, notes, and snippets.

@raws
Created February 21, 2018 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raws/8635acddae1ede48c34198e5e56a58ab to your computer and use it in GitHub Desktop.
Save raws/8635acddae1ede48c34198e5e56a58ab to your computer and use it in GitHub Desktop.
Demonstration of how to create an #each method on our own class that behaves similarly to Array#each
class PumpkinCollection
def initialize(colors)
@colors = colors
end
def each
i = 0
while i < @colors.length do
color = @colors[i]
if block_given?
yield color, 'big'
end
i += 1
end
end
end
pumpkins = PumpkinCollection.new(%w(brown orange yellow red))
pumpkins.each do |color, size|
puts "Hello #{size} #{color}!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment