在 Ruby 中使用元组? [英] Using Tuples in Ruby?

查看:75
本文介绍了在 Ruby 中使用元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人在 Ruby 中使用元组吗?如果是这样,如何实现元组?Ruby 散列非常好,工作起来也差不多,但我真的很想看到像 Python 中的 Tuple 类这样的东西,您可以在其中使用 . 符号来查找您要查找的值.我想要这个,以便我可以创建 D,类似于 Python 的 Dee.

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 中使用元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆