ruby - 如何: Ruby Range that doesn't include the first value

标签 ruby

使用 Ruby 中的范围,您可以执行 0..5 来包含 0 和 5 之间的所有数字(包括 0 和 5)。您还可以执行 0...5 来包含不包括除 5 以外的相同数字。

(1..5) === 5
=> true
(1...5) === 5
=> false
(1...5) === 4.999999
=> true

有没有办法排除第一个数字而不是最后一个数字以获得这样的结果?

(1...5) === 1
=> false
(1...5) === 1.00000001
=> true

最佳答案

不,没有对此类范围的内置支持。如果此行为是必要的,您可能希望推出自己的 Range 类。

关于ruby - 如何: Ruby Range that doesn't include the first value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4511980/

相关文章:

ruby-on-rails - 生产服务器上的 Rails 安全性

ruby-on-rails - 我如何强制 Rails 使用我想要的 Ruby 版本(由 rbenv 管理)?

ruby-on-rails - 如何测试引发错误的方法的副作用

ruby-on-rails - Railscasts 第 362 集 - 导出到 Excel : How to avoid the warning message given by Excel 2010 when opening the file?

ruby - 在 ruby​​ 模型中调用方法之前

ruby - 使用 Mechanize 下载多线程文件?

ruby - 如何让Ruby捕获线程中的语法错误

ruby-on-rails - 我什么时候需要在 Rails 中重启服务器?

ruby - ThinkingSphinx 在 RSpec 中不起作用

mysql - 将 UTF8 数据从 Rails 应用程序编码到 Mysql 时出现问题