From c004fe7983ea0fda082c1c91c4876eef2cd3845a Mon Sep 17 00:00:00 2001 From: symwell <111290954+symwell@users.noreply.github.com> Date: Thu, 1 Sep 2022 14:16:26 -0400 Subject: [PATCH] parse comments backwards in extract_last_comment --- lib/method_source/code_helpers.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/method_source/code_helpers.rb b/lib/method_source/code_helpers.rb index ccf054a..9ab2c73 100644 --- a/lib/method_source/code_helpers.rb +++ b/lib/method_source/code_helpers.rb @@ -104,19 +104,19 @@ def extract_first_expression(lines, consume=0, &block) # @param [Array] lines # @return [String] def extract_last_comment(lines) - buffer = "" + buffer = [] - lines.each do |line| - # Add any line that is a valid ruby comment, - # but clear as soon as we hit a non comment line. + lines.reverse_each do |line| + # Add any line that is a valid ruby comment, and stop as + # soon as we hit a non comment line. if (line =~ /^\s*#/) || (line =~ /^\s*$/) - buffer << line.lstrip + buffer.append(line.lstrip) else - buffer.replace("") + break end end - buffer + buffer.reverse.join() end # An exception matcher that matches only subsets of SyntaxErrors that can be