Ruby 'Range.last' does not give the last value. Why?

后端 未结 2 1628
星月不相逢
星月不相逢 2021-01-12 02:50

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

When I use the \'last\' method I get

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 03:52

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

    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]
    

提交回复
热议问题