Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Latest commit

 

History

History
165 lines (113 loc) · 2.41 KB

ruby.md

File metadata and controls

165 lines (113 loc) · 2.41 KB

Ruby

Naming

  • use snake_case for methods and variables.

  • use CamelCase for classes and modules.

  • Constants are written in all uppercase with underscores to separate words. like: A_CONST

Syntax

  • Always use && and || instead of and and or keywords.

  • Do not use parentheses around the condition of an if/unless/while.

    # bad
    if (x > 5)
      # do something
    end
    
    # good
    if x > 5
      # do something
    end
  • Never use then for multi-line if/unless

    # bad
    if some_condition then
      # do something
    end
    
    #good
    if some_condition
      # do something
    end
  • For single if/unless bolck write it in a single line.

    # bad
    if some_condition
      do_something
    end
    
    # good
    do_something if some_condition
  • Never use unless with else. Write it inside if-else block

    # bad
    unless success?
      puts "failure"
    else
      puts "success"
    end
    
    # good
    if success?
      puts "success"
    else
      puts "failure"
    end
  • Use ternary operator (?:) over if/then/else/end constructs for single line conditions.

    # bad
    result = if some_condition then something else something_else end
    
    # good
    result = some_condition ? something : something_else
  • Avoid multi-line ?:(ternary operator), use if/unless instead.

  • Never use for, unless you know exactly why. You can use each instead of for.

    arr = [1, 2, 3]
    
    # bad
    for element in arr do
      puts element
    end
    
    # good
    arr.each { |element| puts element }
  • use %w freely.

    # bad
    arr = ["one", "two", "three"]
    
    # good
    arr = %w(one two three)
  • Avoid return where not required.

    # bad
    def some_method(arr)
      return arr.size
    end
      
    # good
    def some_method(arr)
      arr.size
    end
  • Don't use an empty line at the beginning or end of methods, blocks or conditionals. Use an empty line between methods, blocks and conditionals.

    # bad
    def hello
    
      puts "Hello world"
    end
    
    # good
    def hello
      puts "Hello world"
    end
    
    # bad
    [2, 3, 4].map{ |x| x + 2 }
    if array.empty?
      # do some thing
    end
    
    # good
    [2, 3, 4].map{ |x| x + 2 }
    
    if array.empty?
      # do some thing
    end
  • Use spaces around operators, after commas, colons and semicolons, around { and before }.

  • No spaces after (, [ No spaces before ], )