Questions tagged [ruby-style-guide]
16 questions
                    
                    9
                    
            votes
                
                3 answers
            
        What is the best way to refactor a long string line in ruby?
I have a line of code like this
"#{envelope_quantity} - envelope #{Budget::util_name(envelope_size)} #{Budget::util_name(envelope_paper)} #{Budget::util_name(envelope_color)} #{Budget::util_name(envelope_grammage)}…
         
    
    
        Ramon Marques
        
- 3,046
- 2
- 23
- 34
                    3
                    
            votes
                
                1 answer
            
        Why does the Rubocop default guidelines recommend parentheses in method definitions?
Why does Rubocop / the community-driven Ruby style guide recommend parentheses in method definitions?
def my_method(param1, param2)
end
# instead of
def my_method param1, param2
end
Method calls are allowed with or without parentheses depending on…
         
    
    
        jleeothon
        
- 2,907
- 4
- 19
- 35
                    3
                    
            votes
                
                1 answer
            
        Rubocop JSON: Align the parameters of a method call if they span more than one line
i got a problem with Rubocop in my testing files. At first, this is my code now:
should 'should show user' do
  get user_url(@user),
    headers: @header
  assert_response :success
end
should 'should update user' do
  patch user_url(@user),
   …
         
    
    
        Tobi
        
- 674
- 5
- 20
                    1
                    
            vote
                
                2 answers
            
        Usage of `Object#send` method as a best practice
It is hard to identify the usages of code like below:
[:create, :update, :pause].each { |action| send("to_#{action}") }
Are there any other reasons that this is an anti-pattern?
         
    
    
        anger
        
- 1,018
- 1
- 9
- 25
                    1
                    
            vote
                
                2 answers
            
        rails 4 scope NOTNULL refactor
I had to create a scope to create active jobs, but this feels a little odd and honestly it's tightly coupled to PosgresSQL:
  scope :active, -> { where('reviewed_at NOTNULL and paid_at NOTNULL and end_at >= ?', Date.today) }
Would you write this…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                0 answers
            
        Single line format VS overstepping pagebreak
Ruby sets guidelines for best practice but what happens when two guidelines contradict eachother? Which one do you prefer?
For example..
Option 1 (Not recommended)
if some_condition
  do_something
end
Option 2 (Recommended)
do_something if…
         
    
    
        ARL
        
- 986
- 2
- 12
- 25
                    0
                    
            votes
                
                1 answer
            
        Ruby Style Guide and logging of messages
I'm developing an application where a lot of errors could occur. So I - and the administrators who shall use this application - have a lot of interest in logging all relevant information. But I'm struggling with ruby style guides.
I love the rubocop…
         
    
    
        PascalTurbo
        
- 2,189
- 3
- 24
- 41
                    0
                    
            votes
                
                1 answer
            
        Rails 4 api default limit params do not pass cops
This seems a bit crazy here.  Is there another way to write this function to get the cops to pass on it, or should I just make it exempt?
Code
  def set_default_limit
    params[:limit]  = params[:limit].to_i > 0 ? params[:limit] :…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        How to improve on this Oauth Rubocop code?
I have the following method setup to assist with refreshing Oauth tokens:
module WhiplashOmniAuthentication
  extend ActiveSupport::Concern
  module ClassMethods
    def from_omniauth(auth)
      Rails.logger.debug auth.inspect
     …
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        rubocop string interpolation and size condition
Before I except these two methods I wanted to see if anyone in the community had a better idea to structure these and make the cops pass.  The first one with to_s seems a bit crazy too.  I was thinking of refactoring the other method but that would…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        rails 4 simple form nested attributes multiple models error during update
I am battling an error with nested attributes and trying to fix the cop error at the same time.  So here is the walk through.  A coupon code may be submitted with the form using nested attributes that may affect the price of the job. This only…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        rails 4 rubocop use proc instead of Proc.new
  validates_presence_of :job, if: Proc.new { |data| data.executed_at? }
I have been tinkering with fixing this issue and it continually fails.  If I switch to the recommended proc it chokes on: 
What is the best way to process this and pass the…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        model show refactor multiple locale messages rubocop
As I wrote this method I knew it could be written better, but it's an MVP.  Now I am trying to learn what the experts feel the approach to be and how to solve the cops.  Constant Learning! 
Here is the controller method in question:
  def show
   …
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                3 answers
            
        rubocop app controller function validate param integer use of nil? predicate
I tried rewriting this function numerous ways to get around this error, however, I want to defer to other experts before I disable the cop around it. 
  def numeric?(obj)
    obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
  end
This…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80
                    0
                    
            votes
                
                1 answer
            
        rubocop eval in rake task
Rubocop chokes on the following setup:
desc 'Clear the db data of development records'
task clear: :environment do
  msg('Clearing the development database and rebuilding the default values')
  'job,company'.split(/, ?/).each do |model|
    #…
         
    
    
        Chris Hough
        
- 3,389
- 3
- 41
- 80