0
0

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 1 year has passed since last update.

配列 の様々な メソッド

Posted at

目的

  • どのようなメソッドがあるのかを理解する

ポイント

  • pry の ls を使って配列のメソッドを確認する
  • やりたい処理を言語化し、使えるメソッドを探す

書き方の例

array = [1, 2, 3]

array.shift  #キュー方式
p array

array.unshift(1)  #キュー方式
p array

array.pop  #スタック方式
p array


~実際の表示~
[2, 3]
[1, 2, 3]
[1, 2]

注意するポイント

  • 破壊的メソッド と 非破壊的メソッドがある
  • 非破壊的メソッドでも「!」を使えば元の変数の値を書き換えることができる

具体的な例

numbers = [3, 8, 1, 18, 124, 10, 8, 5, 53, 10]
numbers.uniq!
p numbers
p numbers.sum


names = ["聡", "次郎", "太郎", "小次郎", "武蔵"]

man = "小次郎"

#result = true

result = names.include?(man)  #trueがかえる

if result
  puts "名前が含まれています"
else
  puts "名前が見つかりませんでした"
end

~実際の表示~
[3, 8, 1, 18, 124, 10, 5, 53]
222
名前が含まれています

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?