Sometimes I need to deal with key / value data.
I dislike using Arrays, because they are not constrained in size (it's too easy to accidentally add more than 2 items, plus you end up needing to validate size later on). Furthermore, indexes of 0
and 1
become magic numbers and do a poor job of conveying meaning ("When I say 0, I really mean head
...").
Hashes are also not appropriate, as it is possible to accidentally add an extra entry.
I wrote the following class to solve the problem:
class Pair
attr_accessor :head, :tail
def initialize(h, t)
@head, @tail = h, t
end
end
It works great and solves the problem, but I am curious to know: does the Ruby standard library comes with such a class already?