Tbpgr Blog

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

ActiveSupport | String#at,from,to,first

概要

String#at,from,to,first

詳細

String#at,from,to,first について

String#at,from,to,first

文字列の位置指定抽出関連のメソッド群。

at任意のインデックスの文字を取得
from任意のインデックス以降の文字列を取得
to任意のインデックスまでの文字列を取得
first

サンプル

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

s = "1234567890"

bulk_puts_eval binding, <<-EOS
s.at 0
s.from 0
s.from -2
s.to 0
s.to 0
s.first
s.first 2
s.first -2
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

s.at 0 # => "1"
s.from 0 # => "1234567890"
s.from -2 # => "90"
s.to 0 # => "1"
s.to 0 # => "1"
s.first # => "1"
s.first 2 # => "12"
s.first -2 # => "12345678"