Skip to content

Instantly share code, notes, and snippets.

@nyarly
Created September 13, 2012 19:36
Show Gist options
  • Save nyarly/3717018 to your computer and use it in GitHub Desktop.
Save nyarly/3717018 to your computer and use it in GitHub Desktop.
Enumerable Examples
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]
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