Created
August 16, 2013 06:46
-
-
Save suan/6247795 to your computer and use it in GitHub Desktop.
Simple Infinite Array Ruby implementation
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 InfiniteArray < Array | |
def initialize(value) | |
super(1, value) | |
end | |
def [](index) | |
self.first | |
end | |
end | |
irb(main):011:0> a = InfiniteArray.new('str') | |
=> ["str"] | |
irb(main):012:0> a[999999] | |
=> "str" | |
irb(main):013:0> a.first | |
=> "str" | |
irb(main):014:0> a.last | |
=> "str" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment