Last active
December 15, 2015 13:09
-
-
Save jrunning/5265536 to your computer and use it in GitHub Desktop.
Ever want to combine two or more Enumerables (or anything that responds to `#to_enum`) into a single one, without reading them first? Ruby 1.9+'s Enumerator is pretty badass. Behold!
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
def poor_devs_cat *args | |
args = args.to_enum | |
Enumerator.new do |yielder| | |
enum = args.next.to_enum | |
loop do | |
begin | |
yielder << enum.next | |
rescue StopIteration | |
enum = args.next.to_enum | |
end | |
end | |
end | |
end | |
cat = poor_devs_cat File.open("part1.txt"), File.open("part2.txt"), File.open("part3.txt") | |
cat.each_with_index do |line,idx| | |
puts "#{idx+1}: #{line.upcase}" | |
end |
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
1: BICYCLE BICYCLE BICYCLE | |
2: I WANT TO RIDE MY BICYCLE BICYCLE BICYCLE | |
3: I WANT TO RIDE MY BICYCLE | |
4: I WANT TO RIDE MY BIKE | |
5: I WANT TO RIDE MY BICYCLE | |
6: I WANT TO RIDE IT WHERE I LIKE | |
7: YOU SAY BLACK, I SAY WHITE | |
8: YOU SAY BARK, I SAY BITE | |
9: YOU SAY SHARK, I SAY HEY MAN | |
10: JAWS WAS NEVER MY SCENE | |
11: AND I DON'T LIKE STAR WARS | |
12: YOU SAY ROLLS I SAY ROYCE | |
13: YOU SAY GOD GIVE ME A CHOICE | |
14: YOU SAY LORD I SAY CHRIST | |
15: I DON'T BELIEVE IN PETER PAN | |
16: FRANKENSTEIN OR SUPERMAN | |
17: ALL I WANNA DO IS |
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
Bicycle bicycle bicycle | |
I want to ride my bicycle bicycle bicycle | |
I want to ride my bicycle | |
I want to ride my bike | |
I want to ride my bicycle | |
I want to ride it where I like |
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
You say black, I say white | |
You say bark, I say bite | |
You say shark, I say hey man | |
Jaws was never my scene | |
And I don't like Star Wars |
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
You say Rolls I say Royce | |
You say God give me a choice | |
You say Lord I say Christ | |
I don't believe in Peter Pan | |
Frankenstein or Superman | |
All I wanna do is |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment