Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created January 19, 2023 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/03c30d3686fffab8c20346dfaedfa51b to your computer and use it in GitHub Desktop.
Save havenwood/03c30d3686fffab8c20346dfaedfa51b to your computer and use it in GitHub Desktop.
require 'forwardable'
class Wombat
NO_DEFAULT = Object.new
include Comparable
include Enumerable
extend Forwardable
def_delegators :@list, :[], :[]=, :fetch, :<<, :delete, :size, :<=>
def initialize(size, default = NO_DEFAULT, &block)
@list = parse_args(size:, default:, &block)
end
def inspect = @list
def each(&block)
return enum_for(__method__) unless block_given?
@list.each { block.call(_1) }
end
private
def parse_args(size:, default:, &block)
if default == NO_DEFAULT
return Array.new(size) unless block_given?
else
return Array.new(size, default) unless block_given?
warn 'warning: block supersedes default value argument'
end
Array.new(size) { |index| block.call(index) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment