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

[Ruby] Array#sum メソッド

Posted at

はじめに

sumメソッドってすごく馴染みがあるように思っていたんですが、使い方をみたら、全然使ったことなかったと思いまして、簡単に記事にまとめておこうと思います。

sumメソッド

配列に対して使います。配列に入っている要素を全部がっちゃんこしてくれるイメージ。

[1, 2, 3, 4].sum
=> 10

引数に明示的に指名すると数値以外にも使用できます。
https://docs.ruby-lang.org/ja/latest/method/Array/i/sum.html

なかなか表現が思いつかなかったのでリファレンスの表現をお借りします。

["お", "は", "よ", "う"].sum("")
=> "おはよう"

[[1], [[2]], [3]].sum([])
=> [1, [2], 3]
0
0
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
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?