Created
February 21, 2018 01:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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