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 5 years have passed since last update.

最近知ったメソッド、忘備録①

Posted at

はじめに

Rubyを学習していて、最近知ったメソッドを忘れないために、記録しておく。

最近使ったメソッド

sliceメソッド

配列や文字列から制定した要素を取り出すことができる。

string = "abcde"
str = string.slice(4)
puts str
# => "d"

puts string
# => "abcde"

元の文字列は変わらない。

slice!メソッド

string = "abcde"
str = string.slice!(4)
puts str
# => "d"

puts string
# => "abce"

(!)のつくメソッドを破壊的メソッドという。その名の通り、破壊する。slice!メソッドは元の配列や文字列から、指定した要素を削除する。

scanメソッド

対象の要素から引数で指定した文字列を数え、配列として返す。

str = "abcdabcdabcd"
str.scan("ab")
# => ["ab", "ab", "ab"]

even?メソッド

対象の数値が偶数かどうかを判別し、真偽値を返す。

12.even?
# => true

include?メソッド

指定した要素が配列ないに含まれているか判断し、真偽値を返す。

array = ["ab", "cd"]
array.include?("ab")
# => true
array.include?("ef")
# => false
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?