Skip to content

Commit f7a7bda

Browse files
committed
Merge pull request #4 from codeunion/rubocop-support
Add Rubocop support
2 parents 9b328d7 + e44c2ec commit f7a7bda

40 files changed

+186
-133
lines changed

.rubocop.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Increase permitted ABC size
2+
Metrics/AbcSize:
3+
Max: 20
4+
5+
# Allow methods of up to 20 lines of code
6+
Metrics/MethodLength:
7+
Max: 20
8+
9+
# Don't enforce top-level class and module documentation
10+
Style/Documentation:
11+
Enabled: false
12+
13+
# Don't enforce "loop do" for infinite loops
14+
Style/InfiniteLoop:
15+
Enabled: false
16+
17+
# Don't enforce underscores for large numeric values
18+
Style/NumericLiterals:
19+
Enabled: false
20+
21+
# Don't enforce prefix-free predicate methods
22+
Style/PredicateName:
23+
Enabled: false
24+
25+
# Don't enforce implicit return
26+
Style/RedundantReturn:
27+
Enabled: false
28+
29+
# Don't enforce implicit self
30+
Style/RedundantSelf:
31+
Enabled: false
32+
33+
# Prefer double quoted string literals
34+
Style/StringLiterals:
35+
Enabled: true
36+
EnforcedStyle: double_quotes
37+
38+
# Don't enforce attr_* for trivial readers/writers
39+
Style/TrivialAccessors:
40+
Enabled: false
41+
42+
# Don't enforce %w and %W for word arrays
43+
Style/WordArray:
44+
Enabled: false

bonus/is_fibonacci.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
# http://cl.ly/image/1z3b3y41130b
1111
# This can calculate is_fibonacci?(fib(2**18)) in ~0.9 seconds.
1212

13-
require 'benchmark'
14-
require_relative 'fast_fib'
13+
require "benchmark"
14+
require_relative "fast_fib"
1515

1616
BIG_FIB_INPUT = 2**18
1717

@@ -28,7 +28,7 @@ def is_fibonacci?(n)
2828
# Print out some friendly usage information
2929
def show_usage!
3030
puts "Usage:"
31-
puts " #{$0} <number-to-test>"
31+
puts " #{$PROGRAM_NAME} <number-to-test>"
3232
puts ""
3333
puts "Options:"
3434
puts " -t, --test Run PASS/FAIL tests"
@@ -62,13 +62,13 @@ def run_benchmarks!(iterations = 3)
6262
# Run our test suite
6363
def run_tests!
6464
puts "Testing with Fibonacci inputs"
65-
[0,1,2,3,5,39088169].each do |n|
65+
[0, 1, 2, 3, 5, 39088169].each do |n|
6666
assert_equal(true, is_fibonacci?(n), "is_fibonacci?(#{n}) is true")
6767
end
6868

6969
puts ""
7070
puts "Testing with non-Fibonacci inputs"
71-
[4,6,7,10,39088169 - 1].each do |n|
71+
[4, 6, 7, 10, 39088169 - 1].each do |n|
7272
assert_equal(false, is_fibonacci?(n), "is_fibonacci?(#{n}) is false")
7373
end
7474

@@ -80,8 +80,11 @@ def run_tests!
8080

8181
puts "done"
8282

83-
assert_equal(true, is_fibonacci?(big_fib), "is_fibonacci?(fib(#{BIG_FIB_INPUT})) is true")
84-
assert_equal(false, is_fibonacci?(big_not_fib), "is_fibonacci?(fib(#{BIG_FIB_INPUT}) + 100) is false")
83+
assert_equal(true, is_fibonacci?(big_fib),
84+
"is_fibonacci?(fib(#{BIG_FIB_INPUT})) is true")
85+
86+
assert_equal(false, is_fibonacci?(big_not_fib),
87+
"is_fibonacci?(fib(#{BIG_FIB_INPUT}) + 100) is false")
8588
end
8689

8790
# Helper method for testing purposes
@@ -98,13 +101,13 @@ def assert_equal(expected, actual, msg)
98101
assert(expected == actual, msg)
99102
end
100103

101-
if __FILE__ == $0
104+
if __FILE__ == $PROGRAM_NAME
102105
case ARGV[0]
103-
when '-h', '--help', '', nil
106+
when "-h", "--help", "", nil
104107
show_usage!
105-
when '-b', '--bench'
108+
when "-b", "--bench"
106109
run_benchmarks!
107-
when '-t', '--test'
110+
when "-t", "--test"
108111
run_tests!
109112
else
110113
puts is_fibonacci?(ARGV[0].to_i)

exercises/board.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
# In short, you should imagine an empty chess-like board absent the idea
2121
# that you're playing chess or any other game. You can place and remove
22-
# whatever you want from each square chess pieces, paper scraps, coins, etc. —
23-
# but you can only place one thing on each square.
22+
# whatever you want from each square -- chess pieces, paper scraps,
23+
# coins, etc. -- but you can only place one thing on each square.
2424

2525
# In code, we'll be representing a "piece" by a single-character string like
2626
# "X", "O", "K", "Q", "N", etc.
@@ -49,15 +49,17 @@ def columns
4949
# Place a "piece" on the board at (row, column)
5050
def place(row, column, piece)
5151
if row >= self.rows
52-
raise "Board has #{self.rows} rows and index #{row} is out of bounds."
52+
fail "Board has #{self.rows} rows and index #{row} is out of bounds."
5353
end
5454

5555
if row >= self.rows
56-
raise "Board has #{self.columns} columns and index #{column} is out of bounds."
56+
fail "Board has #{self.columns} columns and " \
57+
"index #{column} is out of bounds."
5758
end
5859

59-
if @board[row][column] != nil
60-
raise "Cannot place #{piece.inspect} at row #{row}, column #{column}: #{@board[row][column].inspect} is already there."
60+
unless @board[row][column].nil?
61+
fail "Cannot place #{piece.inspect} at row #{row}, column #{column}: " \
62+
"#{@board[row][column].inspect} is already there."
6163
end
6264

6365
@board[row][column] = piece
@@ -99,7 +101,7 @@ def print_board(board)
99101
board.columns.times do |column|
100102
piece = board.at(row, column)
101103

102-
if piece == nil
104+
if piece.nil?
103105
piece = " " # Display empty cells as spaces
104106
end
105107

@@ -108,17 +110,16 @@ def print_board(board)
108110

109111
print "\n"
110112
end
111-
112113
end
113114

114115
# Don't change this code, either!
115116
# See http://cl.ly/image/3t450Y271d25 for expected output
116-
if __FILE__ == $0
117-
board = Board.new(4,4)
118-
board.place(0,0,"X")
119-
board.place(1,1,"O")
120-
board.place(2,2,"X")
121-
board.place(3,3,"#")
117+
if __FILE__ == $PROGRAM_NAME
118+
board = Board.new(4, 4)
119+
board.place(0, 0, "X")
120+
board.place(1, 1, "O")
121+
board.place(2, 2, "X")
122+
board.place(3, 3, "#")
122123

123124
print_board(board)
124125
end

exercises/bottles.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
def bottles(start_number)
3030
end
3131

32-
if __FILE__ == $0
32+
if __FILE__ == $PROGRAM_NAME
3333
# What *should* this print?
3434
bottles(5)
3535
end

exercises/commas.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
def commas(num)
2323
end
2424

25-
if __FILE__ == $0
25+
if __FILE__ == $PROGRAM_NAME
2626
# What are the common cases? What are the corner cases?
2727
# Your sanity checks should look like
2828
# p commas(input) == ...expected return value...

exercises/count_in_list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def count_in_list(list, item_to_count)
2121
# 3. A way to add to the running total as you see the item
2222
end
2323

24-
if __FILE__ == $0
24+
if __FILE__ == $PROGRAM_NAME
2525
# I'd advise putting some sanity checks here.
2626
# How else will you be sure your code does what you think it does?
2727
end

exercises/count_max.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@
88
# count_max([10, 1,2,10,10]) == 3
99
# because "10" is the largest number in the list and it occurs 3 times
1010

11-
# This is how we require the max.rb and count_in_list.rb files in the current folder.
12-
# We can now use the "max" and "count_in_list" methods we defined there — justs
13-
# as if we had defined them here!
11+
# This is how we require the max.rb and count_in_list.rb files in the current
12+
# folder. We can now use the "max" and "count_in_list" methods we defined there
13+
# -- as if we had defined them right here!
1414

15-
require_relative './max'
16-
require_relative './count_in_list'
15+
require_relative "./max"
16+
require_relative "./count_in_list"
1717

1818
def count_max(list)
1919
# You can write this using nothing more than the max and count_in_list
2020
# methods that you've already written. You do not HAVE to, but it's worth
21-
# trying. The "requite_relative" statements above make them available to us, here.
21+
# trying. The "requite_relative" statements above make them available to us.
2222
#
23-
# But remember: inelegant, working code is better than elegant, unfinished code.
23+
# But remember: inelegant, working code is better than elegant,
24+
# unfinished code.
2425
end
2526

26-
if __FILE__ == $0
27+
if __FILE__ == $PROGRAM_NAME
2728
# I'd advise putting some sanity checks here.
2829
# How else will you be sure your code does what you think it does?
2930
end

exercises/die.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Classes: Die
22

33
# Your job is to implement the Die#roll instance method. We've hard-coded
4-
# the number of sides and assigned it to the @sides instance variable
4+
# the number of sides and assigned it to the @sides instance variable --
55
# don't change that!
66

77
# Die#roll should return a random number between 1 and the value of
@@ -19,12 +19,11 @@ def roll
1919
end
2020
end
2121

22-
2322
# Don't change any of this code. It's here to tell you when your method isn't
2423
# working. Since we're dealing with random behavior, there's a (small) chance
2524
# that this code will run even if your Die#roll method isn't working.
2625

27-
if __FILE__ == $0
26+
if __FILE__ == $PROGRAM_NAME
2827
roll_count = 20
2928
die = Die.new
3029

@@ -34,7 +33,8 @@ def roll
3433
1.upto(roll_count) do |i|
3534
result = die.roll
3635
unless (1..6).include?(result)
37-
raise "die.roll returned #{result.inspect}, but it must return a number between 1 and 6 (inclusive)"
36+
fail "die.roll returned #{result.inspect}, " \
37+
"but it must return a number between 1 and 6 (inclusive)"
3838
else
3939
puts "Roll #{i}: #{result}"
4040
end

exercises/die_labels.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def prompt(msg)
2020
gets.chomp
2121
end
2222

23-
if __FILE__ == $0
23+
if __FILE__ == $PROGRAM_NAME
2424
# Examples of what we mean by "returning a random label":
2525
# normal_die = Die.new([1,2,3,4,5,6])
2626
# letter_die = Die.new(['A', 'B', 'C', 'D'])
2727
# dot_die = Die.new(['.', '..', '...', '....'])
2828
#
2929
# letter_die.roll would return one of 'A', 'B', 'C', or 'D' at random
3030

31-
eight_ball = Die.new(['Yes', 'No', 'Unclear', 'Absolutely', 'Never', 'Maybe'])
31+
eight_ball = Die.new(["Yes", "No", "Unclear", "Absolutely", "Never", "Maybe"])
3232

3333
loop do
3434
input = prompt("Ask the Magic 8-Ball a question (or type 'quit' to quit)")

exercises/die_sides.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ def roll
2020
end
2121
end
2222

23-
24-
if __FILE__ == $0
23+
if __FILE__ == $PROGRAM_NAME
2524
die_10 = Die.new(10)
2625
die_20 = Die.new(20)
2726
die_6 = Die.new(6)

0 commit comments

Comments
 (0)