Tbpgr Blog

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

Ruby | String | []=

概要

String#[nth] = val
String#self[nth, len] = val
String#self[substr] = val
String#self[regexp, nth] = val
String#self[regexp, name] = val
String#self[regexp] = val
String#self[range] = val

詳細

String#[nth] = val

文字列の nth 文字目を val に置き換える

require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
str="0123456789"
str[0]='@'
str
str="0123456789"
str[2]='@'
str
str="0123456789"
str[-1]='@'
str
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

str="0123456789" # => "0123456789"
str[0]='@'       # => "@"
str              # => "@123456789"
str="0123456789" # => "0123456789"
str[2]='@'       # => "@"
str              # => "01@3456789"
str="0123456789" # => "0123456789"
str[-1]='@'      # => "@"
str              # => "012345678@"
String#self[nth, len] = val

文字列の nth 文字目から len までを val に置き換える

サンプルコード

require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
str="0123456789"
str[0, 2]='@'
str
str="0123456789"
str[2, 2]='@'
str
str="0123456789"
str[-3, 2]='@'
str
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

str="0123456789" # => "0123456789"
str[0, 2]='@'    # => "@"
str              # => "@23456789"
str="0123456789" # => "0123456789"
str[2, 2]='@'    # => "@"
str              # => "01@456789"
str="0123456789" # => "0123456789"
str[-3, 2]='@'   # => "@"
str              # => "0123456@9"
String#[substr] = val

substr にマッチする部分を val に置き換える

サンプルコード

require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
str="0123456789"
str["2"]='@'
str
str="0123456789"
str["234"]='@'
str
EOS

begin
  str="0123456789"
  str["24"]='@'
rescue => e
  # match しないと IndexError になる
  puts e.message
end

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

str="0123456789" # => "0123456789"
str["2"]='@'     # => "@"
str              # => "01@3456789"
str="0123456789" # => "0123456789"
str["234"]='@'   # => "@"
str              # => "01@56789"
string not matched
String#[regexp, nth] = val

regexp正規表現に nth 番目にマッチした文字列を val に置換する

サンプルコード

require 'tbpgr_utils'

str="0123456789"
str[/(\d)/, 0]='@'
puts str
str="0123456789"
str[/(\d)(\d)(\d)(\d)/, 4]='@'
puts str

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

@123456789
012@456789
String#[regexp, name] = val

regexp の名前付きキャプチャの name にマッチした文字列を val に置換する

サンプルコード

require 'tbpgr_utils'

str="tanaka 34 japanese"
str[/(?<name>.+) (?<age>\d{1,3}) (?<nationality>.+)/, :name]='名前潰し'
p str

str="tanaka 34 japanese"
str[/(?<name>.+) (?<age>\d{1,3}) (?<nationality>.+)/, :age]='年齢潰し'
p str

str="tanaka 34 japanese"
str[/(?<name>.+) (?<age>\d{1,3}) (?<nationality>.+)/, :nationality]='国籍潰し'
p str

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

"名前潰し 34 japanese"
"tanaka 年齢潰し japanese"
"tanaka 34 国籍潰し"
String#[regexp] = val

regexp正規表現にマッチした文字列を val に置換する

サンプルコード

require 'tbpgr_utils'

str="hoge hige hage hoge"
str[/hige/]='@'
p str

str="hoge hige hage hoge"
str[/hage/]='@'
p str

str="hoge hige hage hoge"
str[/h.ge/]='@'
p str

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

"hoge @ hage hoge"
"hoge hige @ hoge"
"@ hige hage hoge"
String#[range] = val

サンプルコード

require 'tbpgr_utils'

str="0123456789"
str[0..1]='@'
p str

str="0123456789"
str[2..4]='@'
p str

str="0123456789"
str[-3..-1]='@'
p str


__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

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

出力

"@23456789"
"01@56789"
"0123456@"