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

1 Answer 1


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

def small_children

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

  .slice_before { |element| element.name == 'br' }
  .map { |element| Footnote.new element }
  • 1
    \$\begingroup\$ I like the braces for blocks with return values ala; onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc \$\endgroup\$
    – garrow
    Mar 16, 2012 at 3:16
  • \$\begingroup\$ @garrow that's an excellent standard to have. I will be adopting that. \$\endgroup\$ Mar 16, 2012 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\$ May 3, 2012 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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