jeudi 29 mars 2018

Ruby's if / else in methods

Currently learning Ruby and curious if it's idiomatic / preferred to always use an else when using if/else in a method.

For example, in JS, this is stylistically acceptable (and sometimes preferred)

function jsFunc(x) {
  if (x == 0) {
    return "Yes";
  }

  return "No";
}

and I've also seen a similar style in Python:

def py_func(x):
    if x == 0:
        return "Yes"
    return "No"

but from the Ruby I've seen, it's always:

def ruby_method(x)
  if x == 0
    "Yes"
  else
    "No"
  end
end

I've also gotten a comment that not using an else in Ruby seems messier? Do Rubyists prefer the "explicit" else? (Also curious a lot of the language is implicit.) Thanks!

Aucun commentaire:

Enregistrer un commentaire