All Questions
Tagged with refactoring ruby
439 questions
0
votes
2
answers
308
views
Refactoring Rails scope method CASE-WHEN-THEN-ELSE-END in MySQL
I'm using MySQL and upgrading my application to Rails 7. In doing so, I've to refactor some order-related queries in my scope methods.
Before of Rails 7 I was using the following scope method (call it ...
0
votes
2
answers
150
views
DRY way of assigning new object's values from an existing object's values
I created a class method that is called when a new object is created and copied from an old existing object. However, I only want to copy some of the values. Is there some sort of Ruby shorthand I can ...
0
votes
2
answers
188
views
How can I replace an instance variable with another one conditionally?
I am assigning values to an instance variable of a class. The scenario is that I need to call a function at least three times, and each call requires an answer to be stored in another instance ...
0
votes
1
answer
78
views
Refactoring nasty rails controller
I've been adding stuff a bit "blindly" to my controller and currently everything is working as it should, but the controller is gotten very messy. I'm using the gem rails_best_practices to ...
-3
votes
1
answer
100
views
A better method to iterate over an array and object attributes? [closed]
(Edit:Now i just use ChatGPT for coding stuff... 1000x better than SO policy and moderators)
I found a solution that works, but i did some heavy copy/paste of blocks and there is only the params of ...
0
votes
1
answer
328
views
Set a Ruby class variable, then use it in a class method. HTTParty
I am trying to create an API wrapper for recharge (A Shopify subscription service), I am using the HTTParty gem
module RechargeAPI
require 'httparty'
BASE_URI = 'https://api.rechargeapps.com'
...
0
votes
0
answers
50
views
Refactor Rails controller
I am trying to achieve this feature:
Sent texts out to new authors.
It enables admin users to
when authors published their first article in a specified timeframe, Compose and send out emails to ...
2
votes
1
answer
76
views
Stuck with refactoring classes on ruby on rails 5
I have a class with different methods but on these methods i need to do a check on the access token before doing some calls
class SomeClass
def initialize
@client = SomeModule::Client.new
...
1
vote
1
answer
1k
views
Metrics/ParameterLists: Method has too many optional parameters. [4/3] Ruby
All params are required
class Library
attr_accessor :authors, :books, :orders, :readers
def initialize(authors = [], books = [], orders = [], readers = [])
@books = books
@...
1
vote
1
answer
231
views
Metrics/AbcSize: Assignment Branch Condition size for fill_arrays is too high. [<9, 21, 0> 22.85/17]
I need to generate random data with ffaker and have no clue how to fix abcsize here
Here is what i had
class Library
attr_accessor :authors, :books, :orders, :readers
def initialize(authors = [], ...
1
vote
1
answer
202
views
How to pass array as individual arguments in ruby?
I am trying to think of a more elegant way to pass elements of an array as individual arguments to a method.
So far, I have the following code:
def fetch_data(keywords)
Product.where("name ...
0
votes
1
answer
267
views
Ruby on rails, Rubocop refactoring
Offenses:
app/controllers/tasks_controller.rb:12:3: C: Metrics/AbcSize:
Assignment Branch Condition size for create is too high. [<3, 19, 1>
19.26/17] def create ...
app/controllers/...
1
vote
1
answer
217
views
Minesweeper game board | ruby
I have written a code for the drawing board for the minesweeper game, can anyone help me to refactor this code more.
Please find my code below
def draw(height, width, mines)
board = Array.new(height)...
0
votes
2
answers
42
views
Creational adapter
I have a lot of code like this
additional_params = {
date_issued: pending.present? ? pending.date_issued : Time.current,
gift_status: status,
date_played: status == "Opened" ? Chronic....
1
vote
2
answers
49
views
Is there a way to refactor this without using 3 collectors to combine strings?
I'm trying to refactor the following:
def method_name
array = ["abcdef", "ghijkl", "mnopqr"]
collector1 = ""
collector2 = ""
...