Created
May 5, 2017 15:31
-
-
Save wconrad/fc7d5a608bc96afb1cfe8c28b4d17613 to your computer and use it in GitHub Desktop.
How to wrap an array in Ruby
This file contains hidden or 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
require "forwardable" | |
# How to wrap an array | |
class Foo | |
extend Forwardable | |
def_delegator :@a, :each | |
include Enumerable | |
def initialize | |
@a = [] | |
end | |
def grow | |
@a << 0 | |
end | |
def has_two? | |
@a.size == 2 | |
end | |
end | |
foo = Foo.new | |
foo.grow | |
p foo.has_two? # => false | |
foo.grow | |
p foo.has_two? # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment