ruby-style-guide icon indicating copy to clipboard operation
ruby-style-guide copied to clipboard

Multi-line method calls with multi-line block

Open arr-dev opened this issue 11 years ago • 4 comments

I have a dilemma regarding styling this peace of code.

Should end on the last line be aligned with the start of the initial line or with the .map call? It seems logical to be with .map but it looks better with initial line.

(2.weeks.ago.to_date..Date.today)
  .map do |date|
    time = date.to_time
    for_day(time)
  end

vs.

(2.weeks.ago.to_date..Date.today)
  .map do |date|
    time = date.to_time
    for_day(time)
end

arr-dev avatar Mar 21 '14 16:03 arr-dev

end should be aligned with map because that's what's initiating the block. It's more logical that way and it also looks better in my opinion.

TSMMark avatar Mar 21 '14 17:03 TSMMark

Personally, I'd write it like this:

(2.weeks.ago.to_date..Date.today).map do |date|
  time = date.to_time
  for_day(time)
end

But if you had to start the map on a new line I'd go with the first option.

Ajedi32 avatar Mar 28 '14 18:03 Ajedi32

Yes, but this is just a simplified example. First option is better but it just looks weird when followed by "end" of method.

arr-dev avatar Mar 28 '14 18:03 arr-dev

collection.
  select do |item|
    ... # 10 lines
  end.
  map do |item|
    ... # 10 lines
  end.
  reduce({}) { |result, item| ... }.
  tap { |result| ... }

LavirtheWhiolet avatar Sep 08 '15 18:09 LavirtheWhiolet