Home > katas > Multiply Ruby (8kyu)

Multiply Ruby (8kyu)

Let's start with Multiply kata. I have to solve this one when I signed up for the Codewars account. It is some kind of validation, I guess.

Chek kata on Codewars


The code does not execute properly. Try to figure out why.

We start with this code:

def multiply(a b)
  a * b

We can solve it just by adding ,

def multiply(a, b)
  a * b

Or we can add return to the function as well

def multiply(a, b)
  return a * b

But most people tend to go with the first solution.

Let's check others solutions.

There is a solution which uses define_method. I don't see any practical reason to use it in this case, but I like its readability.

define_method(:multiply) { |a, b| a * b }

define_method allows you to add methods to your classes in runtime.

© 2021, Andrew Losseff