Tbpgr Blog

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

ActiveSupport | Array#accessing

概要

Array#accessing

詳細

Array#accessing について

Array#accessing

配列の任意の位置にアクセスします。

サンプル

# encoding: utf-8
require 'active_support/core_ext/array/access'
require 'tbpgr_utils'

I18n.enforce_available_locales = true
ary = [*1..43]
bulk_puts_eval binding, <<-EOS
ary.from 40
ary.to 2
ary.second
ary.third
ary.fourth
ary.fifth
ary.forty_two
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

ary.from 40 # => [41, 42, 43]
ary.to 2 # => [1, 2, 3]
ary.second # => 2
ary.third # => 3
ary.fourth # => 4
ary.fifth # => 5
ary.forty_two # => 42