LoginSignup
0
3

More than 3 years have passed since last update.

配列の中の数字の数を数えるメソッド

Posted at

配列の中に複数の数字を入れて9だけカウントして、その数を出力させるメソッドを作る。


numbers = [9, 2, 5, 5, 9, 3, 9, 2, 4, 3]

def array_count9(nums)
  count = nums.count(9)
  puts count
end

array_count() 関数は、配列内の要素の数を返すメソッドである。
処理の流れは、配列内の9という数字の数をか添えて、countという変数の中に代入する。そして、putsメソッドでcountという変数の中に代入された数字を出力する。

<参考記事>

https://ref.xaio.jp/ruby/classes/array/count
https://www.ibm.com/support/knowledgecenter/ja/SSULQD_7.1.0/com.ibm.nz.sqltk.doc/r_sqlext_array_count.html
https://www.ibm.com/support/knowledgecenter/ja/SSULQD_7.1.0/com.ibm.nz.sqltk.doc/r_sqlext_array_count.html

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