Take the 2-minute tour ×

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.

share|improve this question
Did you ever implement D in Ruby? If so, do you have the link to it? I've been working on something similar recently, and I'd love to see what you've done so far. –  dkubb Dec 15 '09 at 9:10
Nope; I never made it that far. I've been winding my way around several other things trying to get back to it. I did find several libraries that seem as though they would help: LazyList and arel. I basically came to the conclusion that LINQ in .NET was almost there, then found arel, which was also close. Using LazyList and removing the direct-to-SQL conversion, the latter of which is also a project goal, would almost get you there. That said, I would love to see what you have so far. I'm still a little way off from getting back to it. –  user29439 Dec 19 '09 at 5:32
ambition is another interesting looking library, but it hasn't been updated in some time. That was the one I found first. arel looks like it has continued in the same tradition. –  user29439 Dec 19 '09 at 5:57
I've just begun a project called veritas to work on this: github.com/dkubb/veritas It's still really early. If things go well I may update DataMapper to use it as a foundation. I'm not really interested in supporting Ambition-like syntax in the core, since you can only use ParseTree with 1.8, but I wouldn't have a problem with a plugin like the one I wrote for DataMapper: github.com/dkubb/dm-ambition –  dkubb Jan 4 '10 at 7:41
I should add that I'm considering updating the underlying implementation to use Struct objects for the tuples to optimize memory usage, but at the moment I'm more concerned about correctness and speccing out the public API. –  dkubb Jan 4 '10 at 7:43
add comment

3 Answers

up vote 19 down vote accepted


Brief example:

require 'ostruct'

person = OpenStruct.new
person.name    = "John Smith"
person.age     = 70
person.pension = 300

puts person.name     # -> "John Smith"
puts person.age      # -> 70
puts person.address  # -> nil
share|improve this answer
That is exactly what I was looking for. Thanks! –  user29439 Feb 8 '09 at 22:00
np :) to answer you question though: no i do not use tuples in ruby, as openstructs or otherwise. i use classes at the high end and hashes at the low end :) –  Iraimbilanja Feb 9 '09 at 11:01
You may like: github.com/papercavalier/structure –  Hakan Ensari Feb 9 '11 at 16:39
this is a poor answer, just a link? Seriously? what happens if/when the link becomes broken? –  omouse Oct 30 '13 at 22:26
add comment

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 the Struct class. eg:

Person = Struct.new(:name, :age)
me = Person.new
me.name = "Guy"
me.age =  30
share|improve this answer
That's close, but having to name it bugs me. I was looking for something like the (1. "a") sort but with the property get/set notation you describe. –  user29439 Feb 8 '09 at 22:02
@panesofglass, there's no need to name nothing: a = Struct.new(:name, :age).new; a.name = "Guy" –  paradoja Feb 9 '09 at 0:56
Can I set a = Struct.new(:name, :age) and later say a.new? I would suppose so. I'll have to check that out. It would be a lot more explicit as to what I want. –  user29439 Jul 26 '10 at 22:02
Finding this years later, I can confirm that Person.new("Guy", 30) does work in addition to setting the fields individually. –  Greg Haskins Apr 29 at 11:42
add comment

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.


:linenum > (a,b,c) = [1,2,3]
:linenum > a
  => 1
:linenum > b
  => 2
:linenum > c
  => 3
share|improve this answer
add comment

Your Answer

