Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 730 Bytes

8 kyu - Basic Mathematical Operations.md

File metadata and controls

34 lines (30 loc) · 730 Bytes

Task

Your task is to create a function that does four basic mathematical operations.

The function should take three arguments - operation(string/char), value1(number), value2(number). The function should return result of numbers after applying the chosen operation.

My solution

def basic_op(operator, value1, value2)
  if operator == '+'
    return value1 + value2
  elsif operator == '-'
    return value1 - value2
  elsif operator == '*'
    return value1 * value2
  else operator == '/'
    return value1 / value2
 end
end

Better solution

def basic_op(operator, value1, value2)
  value1.send(operator, value2)
end

Alternative solution

def basic_op(o,a,b)
  eval [a,o,b].join
end