← Back to Ruby Operators

Multiplication Operator

The multiplication operator (*) can multiple (some) objects. Yep, objects, not just numbers. This, of course, depends on the Object#* method being implemented for that particular object.

Let's look at a few useful examples of multiplying things.

# Integer multiplication
> 5 * 4
=> 20

# String multiplication
> 'ha' * 5
=> "hahahahaha"

# Array multiplication
> (["Ho!"] * 3).join(' ')
=> "Ho! Ho! Ho!"

We can multiple strings when quickly putting together some puts debugging statements.

puts '#' * 60
puts "Debug: #{record.errors}"
puts '#' * 60

References