Domanda

In Ruby, you can omit the parentheses on method calls and also omit the return keyword in methods, as returning the last statement is implicit. For the experienced Ruby programmers, what is the primary trend you've seen and used regarding parentheses usage for method calls and return statements at the end of methods?

È stato utile?

Soluzione

Always include parenthesis when it clearly aids in code readability and omit when it doesn't add clarity.

def foo bar
  string = "hello from foo method"
  bar #omit return keyword
end
foo "cookie" 

def foo(bar)
  string = "hello from foo method"
  return bar
end
foo("cookie")

Both method defs/calls are acceptable. I prefer the first as parans clutter up code in my opinion and most in the Ruby community would agree. However you use, what you're comfortably with, but just keep it consistent.

bar is returned no need to use return keyword. The last statement executed in a method will always be returned.

Links included for further learning:

Neither are official Ruby documents but both can be referenced for general consensus of how to write and format your code as per community preferences.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top