3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyの破壊的メソッドで[!]が付いていないものまとめ

3
Posted at

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/

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?