Ruby : Multiple way of multiplication

How to do multiplication in ruby using different concepts

  1. Regular "*" operator

    3 * 3
    # => 9
  2. Inject *

    [1, 2, 3].inject(:*)
    # => 6
    [1, 2, 3].reject(&:zero?).inject(:*) # if 0's
  3. Reduce

    [1 ,2 ,3].reduce { |sum, x| sum + x }
    # => 6

Last updated