Skip to content

Commit

Permalink
trying to reduce "complexity" of #raise_inspect (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
korny committed Jun 10, 2013
1 parent c386e04 commit 65a4b4d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/coderay/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def reset_instance
SCAN_ERROR_MESSAGE = <<-MESSAGE
***ERROR in %s: %s (after %d tokens)
***ERROR in %s: %s (after %s tokens)
tokens:
%s
Expand All @@ -311,8 +311,8 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle
raise ScanError, SCAN_ERROR_MESSAGE % [
File.basename(caller[0]),
msg,
tokens.respond_to?(:size) ? tokens.size : '[tokens.size undefined]',
tokens.respond_to?(:last) ? tokens.last(10).map(&:inspect).join("\n") : '[tokens.last undefined]',
tokens_size,
tokens_last(10).map(&:inspect).join("\n"),
line, column, pos,
matched, state || 'No state given!',
bol?, eos?,
Expand All @@ -321,6 +321,14 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle
], backtrace
end

def tokens_size
tokens.size if tokens.respond_to?(:size)
end

def tokens_last n
tokens.respond_to?(:last) ? tokens.last(n) : []
end

# Shorthand for scan_until(/\z/).
# This method also avoids a JRuby 1.9 mode bug.
def scan_rest
Expand Down

0 comments on commit 65a4b4d

Please sign in to comment.