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?

引数に配列を入れて、配列の要素に1から順番に番号を割り振るメソッド

Posted at

自身で考えたコード

def print_names(names)

  #eachで配列の要素をnameに格納
  #with_indexメソッドは配列の先頭要素に0から順番に数字を割り振り、idに格納
  
  names.each_with_index do |name, id|
  
  #先頭要素に1を割り当てたいのでid+1
  
  puts "#{id+1}. #{name}"
  end
end

#メソッドの実行
print_names(['上田', '田仲', '堀田'])

出力結果

  1. 上田
  2. 田仲
  3. 堀田
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?