3

What's the general reccomendation for styling and indentation in ruby when you have a method call, with arguments on multiple lines, and a block. For example

collection :available_surveys,
  exec_context: :decorator,
  class: Survey,
  skip_render: lambda {|object, opts| opts[:show_all_surveys] != true } do
    property :name, as: :survey_name
    property :id
  end

The method is collection, it has 4 arguments spread across multiple lines, and then a block argument. One of my colleagues feels the indentation style above makes the block look like it's tied to the last argument, rather than the collection method. I couldn't find any clear style recommendations on the internet.

AndrewSwerlick
  • 913
  • 8
  • 24
  • This question belongs on http://codereview.stackexchange.com – Phrogz Jan 21 '15 at 20:14
  • I think I disagree, based on this from the codereview rules under the off topic section. "Best practices in general (that is, it's okay to ask "Does this code follow common best practices?", but not "What is the best practice regarding X?")". Maybe my wording wasn't clear, but I'm asking a general question about multiline parameters followed by a block. I did provide some example code, but it's more about the general approach than the specific code – AndrewSwerlick Jan 21 '15 at 20:20
  • Forgot to add, if you have recommendations on how to edit it to make that more clear, please let me know. – AndrewSwerlick Jan 21 '15 at 20:27
  • I respect your disagreement. :) I feel that it does not belong on this site because it falls under the [questions-to-avoid](http://stackoverflow.com/tour) category: "Requests for lists of things, polls, **opinions**, discussions, etc.". There is no "official" style guide for Ruby, so there can be no correct answer to this. You're (reasonably) asking for personal preferences—and asking the question well. It's just off-topic for SO, IMHO. – Phrogz Jan 21 '15 at 20:28
  • 1
    I see your point, though I'm not sure how it wouldn't apply to any ruby style related question. Ultimately all style questions are primarily opinion based, particularly in ruby where there is nothing official. If we close this question, why not close all the related questions on the right? These in particular seem very similar, and are highly up voted questions http://stackoverflow.com/questions/616037/ruby-coding-style-guidelines?rq=1 http://stackoverflow.com/questions/5587264/do-end-vs-curly-braces-for-blocks-in-ruby?rq=1 – AndrewSwerlick Jan 22 '15 at 15:28
  • A grey area, for sure. :) – Phrogz Jan 22 '15 at 15:46
  • Maybe it would be better for me to phrase the question as something like "is there a general consensus in the ruby community for how to style x." Then the answer is a little less subjective since it's either yes or no. Thoughts? – AndrewSwerlick Jan 22 '15 at 17:54

1 Answers1

5

For such a complicated method call, I would build the arguments separately and splat them:

collection_args = [
  :available_surveys,
  {
    exec_context: :decorator,
    class: Survey,
    skip_render: lambda {|object, opts| opts[:show_all_surveys] != true }
  }
]
collection *collection_args do
  property :name, as: :survey_name
  property :id
end
Max
  • 21,123
  • 5
  • 49
  • 71
  • I like this. I'm curious if you are aware of this practice being codified in a documented style guide somewhere, or if it's just something you've personally developed. – AndrewSwerlick Jan 21 '15 at 20:05
  • 1
    Or perhaps `collection :available_surveys, survey_options do ... end` – Phrogz Jan 21 '15 at 20:39
  • @AndrewSwerlick The only Ruby style guide I know of is the unofficial one on github and it doesn't mention this case. However, splitting complicated single-line expressions into multiple lines is a pretty standard refactoring technique across all programming languages. – Max Jan 22 '15 at 13:31