ruby-style-guide
ruby-style-guide copied to clipboard
Multi-line method calls with multi-line block
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
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.
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.
Yes, but this is just a simplified example. First option is better but it just looks weird when followed by "end" of method.
collection.
select do |item|
... # 10 lines
end.
map do |item|
... # 10 lines
end.
reduce({}) { |result, item| ... }.
tap { |result| ... }