Ruby 'Range.last' 没有给出最后一个值.为什么? [英] Ruby 'Range.last' does not give the last value. Why?

查看:47
本文介绍了Ruby 'Range.last' 没有给出最后一个值.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ruby​​ Range 对象中使用三点表示法时,我得到:

When using the triple dot notation in a ruby Range object, I get this:

(0...5).each{|n| p n}
0
1
2
3
4

当我使用last"方法时,我得到:

When I use the 'last' method I get:

(0...5).last
 => 5 

我本来期望 4

这是一个错误吗?或者我对 Range 对象的概念有什么不明白的地方?

Is this is a bug? Or is there something I don't understand about the the concept of a Range object?

推荐答案

这是设计使然.Ruby 2.0 文档更具体:

This is by design. The Ruby 2.0 documentation is more specific:

请注意,没有参数 last 将返回定义范围结束的对象,即使 exclude_end?true.>

Note that with no arguments last will return the object that defines the end of the range even if exclude_end? is true.

(10..20).last      #=> 20
(10...20).last     #=> 20
(10..20).last(3)   #=> [18, 19, 20]
(10...20).last(3)  #=> [17, 18, 19]

这篇关于Ruby 'Range.last' 没有给出最后一个值.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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