Skip to content

Instantly share code, notes, and snippets.

@vitaly
Created June 19, 2012 08:09
Show Gist options
  • Save vitaly/2952933 to your computer and use it in GitHub Desktop.
Save vitaly/2952933 to your computer and use it in GitHub Desktop.
class C
def initialize(x)
@x = x
end
def each(&block)
@x.times(&block)
end
include Enumerable
end
# example simplistic implementation for Enumerable
module Enumerable
def map(&block)
res = []
each do |el|
res << block.call(el)
end
res
end
def select(&block)
res = []
each do |el|
res << el if block.call(el)
end
end
def group_by(&block)
res = {}
each do |el|
key = block.call(el)
res[key] ||= []
res[key] << el
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment