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.

推荐答案

OpenStruct?

简单示例:

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

Ruby相关问答推荐

如何通过语法将Ruby Sorbet签名突出显示为不强调的 comments ?

Ruby 中无法解释的撬动行为

Ruby 和用正则表达式分割字符串

Rubymine - 启用行号

如何增加 ruby​​ 应用程序的堆栈大小.递归应用程序获取:堆栈级别太深(SystemStackError)

如何通过 Rack 提供静态文件?

我可以在 Ruby 中使用默认值创建一个数组吗?

将 2 元素数组的数组转换为哈希,其中重复键附加附加值

如何在不按 Enter 的情况下获取单个字符?

安装 RVM:需求安装失败,状态为:1

如何从最后一个元素开始遍历数组?

Ruby 多维数组

对于基于 GitHub 的 gem,强制Bundle 安装使用 https:// 而不是 git://

Ruby中的每个块都可以有一行吗?

在 OSX 10.10 Yosemite 上安装 Nokogiri

如何只获取没有命名空间的类名

如何在 Ruby 中将类构造函数设为私有?

运行 Ruby 命令时,PATH 中不安全的世界可写目录 /Users/username,模式 040777

正则表达式中的 `?i` 和 `?-i` 是什么意思?

如何在 Rails 应用程序中使用 httparty 的基本身份验证?