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 class in Python, where you can use .
notation to find the value for which you are looking. I'm wanting this so that I can create an implementation of D, similar to Dee for Python.
相关问题
- C#: How do i get 2 lists into one 2-tuple list in
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
相关文章
- Ruby using wrong version of openssl
- Given a list and a bitmask, how do I return the va
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
OpenStruct?
Brief example:
You can mock the Scala tuples with this trick :
but here you can't have destructuring:
But thanks to this trick : https://gist.github.com/stevecj/9ace6a70370f6d1a1511 destructuring will work:
Based on the fact that you talk about hashes and . notation I'm going to assume you mean a different kind of tuple than the
(1. "a")
sort. You're probably looking for theStruct
class. eg:I'm the author of Gem for Ruby tuples.
You are provided with two classes:
Tuple
in generalPair
in particularYou can initialize them in different ways:
Both of the classes have some auxiliary methods:
length
/arity
- which returns number of values inside tuplefirst
/last
/second
(only pair) - which returns a corresponding elements[]
that gives you an access to a particular elementsYou can do something similiar with destructuring:
This prints out
3
as expected.While this isn't strictly a tuple (can't do dot notation of members), you can assign a list of variables from a list, which often will solve issues with ruby being pass-by-value when you are after a list of return values.
E.g.