Skip to content

Commit eb75192

Browse files
Refactor indentation checker and check for 2 spaces (#108)
1 parent 2b75af8 commit eb75192

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

lib/analyzers/solution_representation.rb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,13 @@ def initialize(code_to_analyze)
66
@code_to_analyze = code_to_analyze
77
end
88

9-
# REFACTOR: This could be refactored to strip blank
10-
# lines and then use each_cons(2).
119
def indentation_is_sensible?
12-
previous_line = nil
13-
code_to_analyze.lines.each do |line|
14-
# If the previous line or this line is
15-
# just a whitespace line, don't consider it
16-
# when checking for indentation
17-
unless previous_line.nil? ||
18-
previous_line =~ /^\s*\n*$/ ||
19-
line =~ /^\s*\n*$/
10+
code_to_analyze.lines.reject { |line| line =~ /^\s*\n*$/ }.each_cons(2).all? do |lines|
11+
line_1_space = lines.first[/^ */].size
12+
line_2_space = lines.last[/^ */].size
2013

21-
previous_line_lspace = previous_line[/^ */].size
22-
line_lspace = line[/^ */].size
23-
24-
return false if (previous_line_lspace - line_lspace).abs > 2
25-
end
26-
27-
previous_line = line
14+
[0, 2].include? (line_2_space - line_1_space).abs
2815
end
29-
30-
true
3116
end
3217

3318
def has_target_module?

test/exercises/two_fer_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ def self.two_fer(name="you")
336336
"One for %s, one for me." % name
337337
end
338338
end
339-
'].each.with_index do |source, idx|
339+
', '
340+
class TwoFer
341+
def self.two_fer(name="you")
342+
"One for %s, one for me." % name
343+
end
344+
end'].each.with_index do |source, idx|
340345
define_method "test_incorrect_indentation_#{idx}" do
341346
# skip
342347
results = TwoFer::Analyze.(source)

0 commit comments

Comments
 (0)