Skip to content

Instantly share code, notes, and snippets.

@kivanio
Forked from syntruth/tuple.rb
Created September 18, 2011 22:43
Show Gist options
  • Save kivanio/1225668 to your computer and use it in GitHub Desktop.
Save kivanio/1225668 to your computer and use it in GitHub Desktop.
Ruby Tuple Class
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
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