Skip to content
This repository was archived by the owner on Feb 6, 2018. It is now read-only.

Commit 65a4b4d

Browse files
committed
trying to reduce "complexity" of #raise_inspect (rubychan#135)
1 parent c386e04 commit 65a4b4d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/coderay/scanner.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def reset_instance
289289
SCAN_ERROR_MESSAGE = <<-MESSAGE
290290
291291
292-
***ERROR in %s: %s (after %d tokens)
292+
***ERROR in %s: %s (after %s tokens)
293293
294294
tokens:
295295
%s
@@ -311,8 +311,8 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle
311311
raise ScanError, SCAN_ERROR_MESSAGE % [
312312
File.basename(caller[0]),
313313
msg,
314-
tokens.respond_to?(:size) ? tokens.size : '[tokens.size undefined]',
315-
tokens.respond_to?(:last) ? tokens.last(10).map(&:inspect).join("\n") : '[tokens.last undefined]',
314+
tokens_size,
315+
tokens_last(10).map(&:inspect).join("\n"),
316316
line, column, pos,
317317
matched, state || 'No state given!',
318318
bol?, eos?,
@@ -321,6 +321,14 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle
321321
], backtrace
322322
end
323323

324+
def tokens_size
325+
tokens.size if tokens.respond_to?(:size)
326+
end
327+
328+
def tokens_last n
329+
tokens.respond_to?(:last) ? tokens.last(n) : []
330+
end
331+
324332
# Shorthand for scan_until(/\z/).
325333
# This method also avoids a JRuby 1.9 mode bug.
326334
def scan_rest

0 commit comments

Comments
 (0)