-
-
Save shanel/2704533 to your computer and use it in GitHub Desktop.
Ruby Tuple Class
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 Tuple < Array | |
def initialize(*args) | |
@array = args.dup.freeze | |
end | |
def each(&block) | |
@array.each(&block) | |
end | |
def first | |
return @array.first() | |
end | |
def last | |
return @array.last() | |
end | |
def size | |
return @array.size() | |
end | |
def [](idx) | |
return @array[idx] | |
end | |
def to_a | |
return Array.new(@array) | |
end | |
def to_s | |
return "(%s)" % @array.join(", ") | |
end | |
undef << | |
undef concat | |
undef pop | |
undef push | |
undef shift | |
undef unshift | |
undef uniq! | |
undef flatten! | |
end | |
# Update the Array class. | |
class Array | |
def to_tuple | |
return Tuple.new(*self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment