Using Tuples in Ruby?

后端 未结 7 1039
醉话见心
醉话见心 2020-12-24 00:24

Does anyone use tuples in Ruby? If so, how may one implement a tuple? Ruby hashes are nice and work almost as well, but I\'d really like to see something like the Tuple clas

7条回答
  •  半阙折子戏
    2020-12-24 00:51

    I'm the author of Gem for Ruby tuples.

    You are provided with two classes:

    • Tuple in general
    • Pair in particular

    You can initialize them in different ways:

    Tuple.new(1, 2)
    Tuple.new([1, 2])
    Tuple(1, 2)
    Tuple([1, 2])
    Tuple[1, 2]
    

    Both of the classes have some auxiliary methods:

    • length / arity - which returns number of values inside tuple
    • first / last / second (only pair) - which returns a corresponding elements
    • [] that gives you an access to a particular elements

提交回复
热议问题