Skip to content

Instantly share code, notes, and snippets.

@shanel
Forked from kivanio/tuple.rb
Created May 15, 2012 19:47
Show Gist options
  • Save shanel/2704533 to your computer and use it in GitHub Desktop.
Save shanel/2704533 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
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