# Ruby blocks format

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?)


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?)

• I like the braces for blocks with return values ala; onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc – garrow Mar 16 '12 at 3:16
• @garrow that's an excellent standard to have. I will be adopting that. – Mongus Pong Mar 16 '12 at 9:40
• 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. – Wayne Conrad May 3 '12 at 21:57