DEV Community

Discussion on: [Challenge] Add numbers without (+-*/)

Collapse
 
pyrsmk profile image
Aurélien Delogu

Here's another, more serious, attempt (still in Ruby) :

def add(a, b)
  (-a...b).size
end
Enter fullscreen mode Exit fullscreen mode

Some explanations : in Ruby 0..10 is called a range. It is really useful to make arbitrary loops or splice an array, for example. Simply, a range is a suite of values. The syntax I used here is 0...10 (note the three dots) which is an exclusive range : the suite of values goes from 0 to 9. So the trick is to have a range going from -a to b and excluding b because 0 is included in the suite.