Skip to content

Commit 6f72191

Browse files
committed
ci: add ruby style check
1 parent e185685 commit 6f72191

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

.github/workflows/ruby-style.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Ruby Style Check
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
paths:
7+
- '**/*.rb'
8+
9+
jobs:
10+
style:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Ruby
16+
uses: ruby/setup-ruby@v1
17+
with:
18+
ruby-version: '3.4'
19+
bundler-cache: true
20+
21+
- name: Install RuboCop and extensions
22+
run: |
23+
gem install rubocop
24+
gem install rubocop-rake
25+
gem install rubocop-rspec
26+
27+
- name: Run RuboCop
28+
continue-on-error: true
29+
run: rubocop

.rubocop.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require:
2+
- rubocop-rake
3+
- rubocop-rspec
4+
5+
AllCops:
6+
NewCops: enable
7+
TargetRubyVersion: 3.4
8+
9+
Style/Documentation:
10+
Enabled: false
11+
12+
Layout/LineLength:
13+
Max: 120
14+
15+
Style/StringLiterals:
16+
EnforcedStyle: single_quotes
17+
18+
Style/FrozenStringLiteralComment:
19+
Enabled: true
20+
EnforcedStyle: always
21+
22+
Style/For:
23+
Enabled: false

2024/day1/part1.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
# frozen_string_literal: true
2+
13
def functional_style(input)
2-
lefts, rights = input.split("\n").map {
3-
|line| line.split(' ').map(&:to_i)
4-
}.transpose
4+
lefts, rights = input.split("\n").map { |line| line.split(' ').map(&:to_i) }.transpose
55

66
sorted_lefts = lefts.sort
77
sorted_rights = rights.sort
88

9-
distance = sorted_lefts.zip(sorted_rights).sum { |left, right| (left - right).abs }
10-
11-
return distance
9+
sorted_lefts.zip(sorted_rights).sum { |left, right| (left - right).abs }
1210
end
1311

1412
def imperative_style(input)
@@ -18,7 +16,7 @@ def imperative_style(input)
1816
rights = []
1917

2018
for line in lines
21-
left, right = line.split(" ")
19+
left, right = line.split(' ')
2220
lefts << left.to_i
2321
rights << right.to_i
2422
end
@@ -30,13 +28,15 @@ def imperative_style(input)
3028
for i in 0..(lefts.length - 1)
3129
distance += (sorted_lefts[i] - sorted_rights[i]).abs
3230
end
33-
return distance
31+
32+
distance
3433
end
3534

3635
def solution(input)
3736
functional_result = functional_style(input)
3837
imperative_result = imperative_style(input)
3938
puts "Functional result: #{functional_result}"
4039
puts "Imparative result: #{imperative_result}"
41-
return functional_result
40+
41+
functional_result
4242
end

2024/day1/part2.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
# frozen_string_literal: true
2+
13
def functional_style(input)
24
lefts, rights = input.split("\n").map { |line| line.split(' ') }.transpose
35

46
counts = rights.tally
5-
6-
similarity = lefts.sum { |left| counts[left].to_i * left.to_i }
7-
8-
return similarity
7+
lefts.sum { |left| counts[left].to_i * left.to_i }
98
end
109

1110
def imperative_style(input)
@@ -15,7 +14,7 @@ def imperative_style(input)
1514
rights = Hash.new(0)
1615

1716
for line in lines
18-
left, right = line.split(" ")
17+
left, right = line.split(' ')
1918
lefts << left
2019
rights[right] += 1
2120
end
@@ -25,13 +24,14 @@ def imperative_style(input)
2524
similarity += left.to_i * rights[left]
2625
end
2726

28-
return similarity
27+
similarity
2928
end
3029

3130
def solution(input)
3231
functional_result = functional_style(input)
3332
imperative_result = imperative_style(input)
3433
puts "Functional result: #{functional_result}"
3534
puts "Imparative result: #{imperative_result}"
36-
return functional_result
35+
36+
functional_result
3737
end

0 commit comments

Comments
 (0)