LoginSignup
0
1

More than 3 years have passed since last update.

絶対値、配列の何番目、文字(数字)を1つだけ削除、同じ文字連続

Last updated at Posted at 2019-03-11

絶対値を取得する。

−8の絶対値を取得する時
-8.abs => 8

配列内(m)の特定の文字(3)が何番目にあるか?

3は何番目にあるか?
m = [1,2,3,4,5]
m.find_index(3) => 2

0から数えているので1足して、3番目になる。

配列内(m)の特定の文字(3)を一つだけ削除する。

3を1つ削除する
m = [1,2,3,3,3]
m.delete_at(m.find_index(3)) => 3
m = [1,2,3,3]
m.delete_at(m.find_index(3)) => 3
m = [1,2,3]

m.find_index(3)で3番目に3があることを出して、3番目を削除する。

連続(4回)して同じ文字(nil)を入れる

nilを4つ入れたい時

m = Array.new(4){nil} => [nil, nil, nil, nil]
座標を使いたい時など
m = []
6.times{|i| m << Array.new(2){i} } => 6
m => [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]
0
1
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
0
1