Skip to content

Instantly share code, notes, and snippets.

@travisvalentine
Last active January 4, 2016 07:29
Show Gist options
  • Save travisvalentine/8588450 to your computer and use it in GitHub Desktop.
Save travisvalentine/8588450 to your computer and use it in GitHub Desktop.
One method, two languages
# RUBY METHOD
# -----------------------------------
def sum_multiples(min, max)
n = (max - 1) / n
sum = n * (n+1) / 2 * min
end
// SCALA METHOD
// -----------------------------------
def sum_multiples(min: Int, max: Int) : Int = {
var n = (max - 1) / min
var sum = n * (n+1) / 2 * min
return sum
}
// this is one way to run the method above, passing in numbers as variables
sum_multiples(3, 1000)
@travisvalentine
Copy link
Author

The differences:

  • Notice that Scala methods are more explicit. They define the type of variable you pass in (in the above case, Integers), as well as what the output is (also an Integer). You don't need to do that in Ruby.
  • Also, in Scala you see return sum used. Ruby doesn't need those explicit return statments. Sometimes people might add the following to the Ruby method:
def sum_multiples(min, max)
  n = (max - 1) / n
  sum = n * (n+1) / 2 * min
  sum # note you don't have to type return here. since it's the last thing evaluated in the method it's returned
end

Again, sometimes people explicitly return in Ruby but in Scala I believe it's required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment