Created
September 13, 2012 19:36
-
-
Save nyarly/3717018 to your computer and use it in GitHub Desktop.
Enumerable Examples
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 Fibonacci | |
include Enumerable | |
def initialize | |
@this = 1 | |
@next = 1 | |
end | |
def each | |
loop do | |
yield @this | |
@this, @next = @next, @this + @next | |
end | |
end | |
end | |
fib = Fibonacci.new | |
p fib.take(15) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] |
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
puts( (1..110).zip( | |
(-2..0).cycle, | |
(-4..0).cycle, | |
(-6..0).cycle | |
).map do |number, foo, bar, baz| | |
[ number ] + | |
%w{foo bar baz}.zip( | |
[foo, bar, baz]).map do |blub, zero| | |
zero == 0 ? blub : nil | |
end.compact | |
end.map do |output| | |
output.join("") | |
end.join("\n") ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment