Skip to main content

List of Common mistakes in Ruby



Always use spaces around operators, after commas, colons and semicolons, around { and before }. White space might be (mostly) irrelevant to the Ruby interpreter, but its proper use is the key to writing easily readable code. Also it makes the design more readable and code much cleaner.

product = 1 * 2
array = [1, 2, 3, 4, 5]
array.map { |a| a + 2 }


There should be no spaces after (, [ or before ], ) these brackets.

['ankur', 'vyas']
sum(a, b)

Also don't use spaces in while providing the range.

Use 5..9 over 5 .. 9
Use 'a'..'z' over 'a' .. 'z'

When using switch case statements use the following indentation.

case
  when input = '+'
    puts 'The operation is addition'
  when '-'
    puts 'The operation is subtraction'
  when '*'
    puts 'The operation is multiplication'
  else
    puts 'The operation is division'
end

Use following indentation if the parameters exceeds to more than one line.

def send_mail(source)
  Mailer.deliver( to: 'ankurvy1@gmail.com',
                  from: 'someone@someone.com',
                  subject: 'Message subject',
                  body: source.text)
end

Use splat for better assignments.

element_1, *other_elements = ['a', 'n', 'k', 'u', 'r']

If only 2 conditions are present use the ternary operators.

var_1 = condition ? assignment_1 : assignment_2


If you are setting a variable using the if condition the use following syntax.

variable_result = if condition_1
                    a
                  else_if condition_2
                    b
                  else
                    c
                  end

Avoid return statements at the end of the line. In ruby the last line of the method is returned.

Use snake case for all the ruby variables.

variable_1, some_variable

Use camel case for class names.

ClassOne, ClassTwo

Use screaming snake case for the constants' names.

CONSTANT_1, CONSTANT_2


Use %w for the string arrays and %i for the symbol arrays.

STATUS_MAP = %w(open closed draft paid)
SYMBOL_MAP = %i(symbol1 symbol2 symbol3)

Use interpolation over concatenation.

Use "#{string_1} < #{string2} >" over string_1 + '<' + string2 + '>'

Assign single quotes to the string which does not have interpolation.

Use 'string 1' over "string 1"

Do not use the word partial when you don't want to pass some variables in it.

Use render 'partial_1' over render partial: 'partial_1'


Reference:



Comments

Popular posts from this blog

OOP Polymorphism

Definition : " Poly " stands for " many " and " morph " stands for " forms ". Generally, polymorphism means one name different uses . Technically, it means being able to send the same message to different objects and get different result. Polymorphism through Inheritance We can achieve polymorphism through inheritance . For example class GenericParser def parse raise NotImplementedError , 'You must implement the parse method' end end class JsonParser < GenericParser def parse puts 'An instance of the JsonParser class received the parse message' end end class XmlParser < GenericParser def parse puts 'An instance of the XmlParser class received the parse message' end end Here the GenericParser is the base class. Class JsonParser and XmlParser are inherited from the GenericParser. Now suppose we run the below code. puts 'Using the XmlParser' parser

Rails: Grouping the records by group_by method

Many times we come across such situations where we need to group records, like when we want to display grouped messages, email, alerts .etc based on date or time. Here is the simple code which provides a significant use of group_by method. Controller Part class MessagesController < ApplicationController   def index     @message = Message .all     #Retrives all messages and divides into two groups todays messages and other messages     @grouped_messages = @message .group_by{ |t| t.created_at.to_date == DateTime .now.to_date }     if @grouped_messages [ false ].present?       #Create month wise groups of messages             @month_wise_sorted_alerts  = @grouped_messages [ false ].group_by{ |t| t.created_at.month}     end       end end @message .group_by{ |t| t.created_at.to_date == DateTime .now.to_date } The above line return us the messages in Ordered Hash with two keys true and false on which the messages which are of today's date will be valu

Scope and Access modifiers in Ruby

Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers set the visibility of methods and member fields. Ruby has three access modifiers: public , protected and private . In Ruby, all data members are private. Access modifiers can be used only on methods. Ruby methods are public, unless we say otherwise. The scope of the methods is until another scope comes i.e. when we write private all the methods below it are private until any other scope (i.e. protected or public). NOTE: Ruby does not apply any access control over instance and class variables. Public Methods: Public methods can be called by anyone. Methods are public by default except for initialize, which is always private. Private Methods: Private methods cannot be accessed, or even viewed from outside the class. Only the class methods can access private members. Protected Methods: A protected met