0
0

More than 1 year has passed since last update.

sliceメソッドの使用方法

Posted at

sliceメソッドとは

sliceメソッドとは、配列や文字列から指定した要素を取り出すことができるメソッドです。

# 配列を作成します
array = [0,1,2,3,4,5,6]

# 配列から引数で指定した要素を取得します
ele1 = array.slice(1)
puts ele1
#=> 1

# 配列番号-4から4つ分の要素をsliceします
ele2 = array.slice(-4,4)
puts ele2
#=> 3, 4, 5, 6

配列はもとのままです

puts array
#=> [0,1,2,3,4,5,6]

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