1
\$\begingroup\$

Is there a better way to format this code, to make it more readable?

footnotes_paragraph.xpath('./small').children.slice_before do |element|
  element.name == 'br'
end.map do |element|
  Footnote.new element
end.reject(:empty?)
\$\endgroup\$
4
\$\begingroup\$

You could extract out the initial xquery part into a separate method

def small_children
    footnotes_paragraph.xpath('./small').children
end

Instead of using do end you could use {}. Then just a little reformatting, personal preference but for long method chains I like to start each line with a .methodcall

small_children
  .slice_before { |element| element.name == 'br' }
  .map { |element| Footnote.new element }
  .reject(:empty?)
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I like the braces for blocks with return values ala; onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc \$\endgroup\$ – garrow Mar 16 '12 at 3:16
  • \$\begingroup\$ @garrow that's an excellent standard to have. I will be adopting that. \$\endgroup\$ – Mongus Pong Mar 16 '12 at 9:40
  • 1
    \$\begingroup\$ In MRI <= 1.8.7, you'll need a \ on the end of each line (except the last). Ruby 1.9's parser is much better this way. \$\endgroup\$ – Wayne Conrad May 3 '12 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.