Multi-line parameters(arguments) with multiline paraemetrs
I can't find answer to this.
What is considered better style: Solution 1(Close parentheses on new line):
def initialize(arg1,
arg2,
arg3 = Whatever.new,
arg4 = Whatever2.new(arg2_1,
arg2_2,
arg2_3
)
)
, or Solution 2 (Racket style):
def initialize(arg1,
arg2,
arg3 = Whatever.new,
arg4 = Whatever2.new(arg2_1,
arg2_2,
arg2_3))
, or maybe Solution 3(Mixed):
def initialize(arg1,
arg2,
arg3 = Whatever.new,
arg4 = Whatever2.new(arg2_1,
arg2_2,
arg2_3)
)
IMHO Solution 3
Solution 2 seems to be preferred: https://github.com/bbatsov/ruby-style-guide/issues/80
I'm a Python programmer and relatively new to Ruby - just stumbled across this. In my opinion, I like indenting each block of arguments (as in solution 1) but adding a blank line above the first argument of the nested .new call:
def initialize(arg1,
arg2,
arg3 = Whatever.new,
arg4 = Whatever2.new(
arg2_1,
arg2_2,
arg2_3
)
)
or even
def initialize(
arg1,
arg2,
arg3 = Whatever.new,
arg4 = Whatever2.new(
arg2_1,
arg2_2,
arg2_3
)
)
Just my two cents - I like using the indentation to align arguments for the same methods.
@Bartuz, I think what any of your examples are trying to tell you is "Refactor me, now, please!" If I had to pick one of the three options you listed, I'd choose Option 2. I generally find closing parens or braces, as the entire content of a line, an engraved invitation to introduce bugs…an invitation which will be taken up at some point of the project lifecycle.
@Bartuz, Solution 2 does not seem preferable: #80 if we about initialize method It needs to be refactored.
def initialize(*args)
...
end
Here are four general guides you can follow from four reputable sources
rubocop (formatting gem)
https://github.com/rubocop-hq/ruby-style-guide
# good (normal indent)
def send_mail(source)
Mailer.deliver(
to: '[email protected]',
from: '[email protected]',
subject: 'Important message',
body: source.text
)
end
Shopify
https://shopify.github.io/ruby-style-guide/
# good
def send_mail(source)
Mailer.deliver(
to: '[email protected]',
from: '[email protected]',
subject: 'Important message',
body: source.text,
)
end
github
https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md couldn't find the right spot here
airbnb
https://github.com/airbnb/ruby
# good
def self.create_translation(
phrase_id,
phrase_key,
target_locale,
value,
user_id,
do_xss_check,
allow_verification
)