Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Range | end or last

概要

Range#end -> object
Range#last -> object
Range#last(n) -> [object]

詳細

終端の要素を返却。
last に引数 n を付加すると、終端から n 要素を返却

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
(2..5).end
(2...5).end
(2..5).last
(2...5).last
(2..5).last(2)
(2...5).last(2)
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

(2..5).end      # => 5
(2...5).end     # => 5
(2..5).last     # => 5
(2...5).last    # => 5
(2..5).last(2)  # => [4, 5]
(2...5).last(2) # => [3, 4]