Rubyにおける破壊的メソッドの中で、メソッドの最後に[!]が付いていないものをまとめてみました。それと簡単な説明を書いておきました。間違いや抜けがあれば教えてください!
Stringクラス
insert
str.insert(index, other_str)
文字列の中でindexの位置に別の文字列other_strを挿入
replace
str.replace "bar"
self の内容を other の内容で置き換えます。
concat
str.concat(other_str)
<<の別名です。文字列strの末尾に別の文字列other_strを加えます
[]=
str[index]=other_str
strのindex番号を指定して文字を置き換える。
Arrayクラス
push
array.push(obj, ...)
配列の末尾に引数を要素として追加する
concat
array.concat(other_array)
配列arrayの末尾に引数の配列other_arrayを結合する
insert
array.insert(index, obj, ...)
引数indexで指定した位置に引数objを要素として挿入する。第2引数以降に複数の引数を並べると、複数の要素を順に挿入する。
shift
array.shift
配列の最初の要素を削除する。
unshift
array.unshift(obj, ...)
引数objを配列の先頭に要素として追加する。引数を複数指定すると、それぞれ先頭から順に追加する。
[]=
array[index] = obj
[]内で指定した位置の要素を右辺で置き換える。
fill
array.fill(obj)
配列の全ての要素を引数objで置き換える。
replace
array.replace(other_array)
配列の内容を引数other_arrayで置き換える。
delete_at
array.delete_at(index)
配列から引数indexの位置を削除する
delete_if
array.delete_if {|item| block }
要素の数だけ繰り返しブロックを実行し、ブロックの戻り値が真になった要素を削除する。
delete
array.delete(obj)
配列から引数objと同じ要素を探して、全て削除する。
clear
array.clear
要素を全て削除し、配列を空にする。
pop
array.pop
配列の末尾の要素を削除する。
Hashクラス
[]=
hash[key] = val
ハッシュにkeyとvalのペアを追加する。keyがすでにあれば、そのkeyの値をvalで上書きする。
delete
hash.delete(key)
ハッシュから引数keyと同じキーを探して、キーと値を削除する。
delete_if
hash.delete_if {|key, val| block }
繰り返しブロックを実行し、ブロックの戻り値が真になったキーと値を削除する。
replace
hash.replace(other)
ハッシュの内容をotherの内容に置き換える。
shift
hash.shift
ハッシュからキーと値を1組削除する。
update
hash.update(other_hash)
merge!の別名。レシーバhashの内容に引数other_hashの内容を加える。
clear
hash.clear
キーと値を全て削除する。
参考
https://ref.xaio.jp/ruby
http://web-tenna.com/ruby-destruction-method/