Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Numeric | divmod

概要

Numeric#divmod(other) -> [Numeric]

詳細

self を other で割った整数の商 q と余り r を返却。

商はNumeric#div
余りはNumeric#modulo
と同じ値。

サンプルコード
p 5.divmod(2)
p 5 / 2
p 5.div(2)
p 5 % 2
p 5.modulo(2)
p 4.divmod(2)
p 2.divmod(4)

出力

$ ruby numeric_divmod.rb
[2, 1]
2
2
1
1
[2, 0]
[0, 2]