Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?)
share|improve this question

1 Answer 1

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?)
share|improve this answer
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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