Here are a couple of examples of one-liners that go way beyond 80 characters:scope = DepartmentRepository.includes(:location).by_account(@request.account_id).find(approved_department_ids)departments = ActiveModel::ArraySerializer.new(scope, each_serializer: DepartmentWithLocationSerializer).as_jsonI usually simply break these kinds of chained method calls before the . and indent the next line. My IDE (Emacs) runs rubocop on my source files as I edit them and doesn't complain about this practice. It does complain about lines longer than 80 characters so I feel somewhat compelled to fix them. How would I refactor these? The first line is calling ActiveRecord finders and scopes. Scopes can be compbined but then they have ackward names that seldom see reuse (e.g. by_account_with_locations).
How do I refactor lines of Ruby code that run too long due to method chaining or object instantiation?
ruby
I don't really see any real trouble with those lines. But I know the feeling that a line just seems too long (especially if everything else in the file is nice and neat). It's often a good thing to notice, but in some cases it just doesn't make sense to worry.Are the lines readable? Absolutely. They are very descriptive in fact. If you were talking about a very long conditional filled with && and || and negations, then yeah, it should probably be changed. But in this case, I don't see reason to fret.But if you're intent on refactoring, you could (as an example) move the ArraySerializer instantiation into a factory on DepartmentWithLocationSerializer and get something likeDepartmentWithLocationSerializer.serialize(scope).as_jsonThat sort of thing helps a bit. But if I knew a good zen quote about a little imperfection being OK too, I'd drop it here :)Anyway, don't worry about adding methods for things you'll only use once. You don't always need a very practical reason like DRY, to add some more methods. If adding some methods it can make the code a little neater to look at, that's a reason in itself. Everything in moderation, YMMV, etc., so just try it out and see if the code seems nicer afterwards. If it doesn't, well, then try to live with the long lines.