

Ruby: Refactoring a complicated method of nested loop
source link: https://www.codesd.com/item/ruby-refactoring-a-complicated-method-of-nested-loop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Ruby: Refactoring a complicated method of nested loop
I'm trying to get rid of duplication in my code. I have a method that populates a checkerboard with checkers:
def populate_checkers
evens = [0, 2, 4, 6]
odds = [1, 3, 5, 7]
0.upto(2) do |x_coord|
if x_coord.even?
evens.each do |y_coord|
red_checker = Checker.new(x_coord, y_coord, :red)
@board[x_coord][y_coord] = red_checker
end
elsif x_coord.odd?
odds.each do |y_coord|
red_checker = Checker.new(x_coord, y_coord, :red)
@board[x_coord][y_coord] = red_checker
end
end
end
5.upto(7) do |x_coord|
if x_coord.even?
evens.each do |y_coord|
black_checker = Checker.new(x_coord, y_coord, :black)
@board[x_coord][y_coord] = black_checker
end
elsif x_coord.odd?
odds.each do |y_coord|
black_checker = Checker.new(x_coord, y_coord, :black)
@board[x_coord][y_coord] = black_checker
end
end
end
end
How can I remove duplication and still get the precise behavior I need?
You can try to extract a method and then extract a block into lambda. Then your code will be readable and loose of duplication
def populate_checkers
0.upto(2) do |x_coord|
populate_checker(x_coord, :red)
end
5.upto(7) do |x_coord|
populate_checker(x_cord, :black)
end
end
def populate_checker(x_coord, color)
evens = [0, 2, 4, 6]
odds = [1, 3, 5, 7]
apply_checker = lambda do |y_coord|
checker = Checker.new(x_coord, y_coord, color)
@board[x_coord][y_coord] = checker
end
if x_coord.even?
evens.each(&apply_checker)
elsif x_coord.odd?
odds.each(&apply_checker)
end
end
Recommend
-
9
Nested method calls via two-phase borrowing Mar 1, 2017 In my previous post, I outlined a plan for non-...
-
8
10 lessons learnt from the Ruby Refactoring Kata - Tennis Game Over the last ~2 months, I’ve been scheduling some time to work on a specific Ruby code which is designed to be a good starting point for a refactoring. Those...
-
7
nested for the loop checker advertisements here is my code Im kinda new to programming any way the goal of the program is to out put all the primes...
-
10
Nested While Loop in Java It is pretty impossible to impose hundreds of words or numbers by just writing or putting into variable one by one. Instead, use a loop to avoid extra time. This article gives...
-
10
Since Rails 5.2 was released, we can make use of the Credentials API to manage our secrets. Check out our previous blog on Rails cred...
-
10
Copy link Member ghiculescu commented...
-
14
Alexander ZeitlerRefactoring-safe nested validation with express-validator and ts-simple-nameofPublished on Monday, August 9, 2021
-
8
-
7
Refactoring the "Legacy" Hudson.java with the Mikado Method as a Coding Dojo April 16, 2011 I'm preparing a coding dojo for my...
-
9
FragmentsSorbet + 100% cov makes Ruby refactoring possibleLast week, we deployed a refactoring patch to our Ruby codebase that was big for us
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK