Skip to content

Instantly share code, notes, and snippets.

@ParthBarot-BoTreeConsulting
Last active October 14, 2015 17:34
Show Gist options
  • Save ParthBarot-BoTreeConsulting/4d6373d396eb46e156ed to your computer and use it in GitHub Desktop.
Save ParthBarot-BoTreeConsulting/4d6373d396eb46e156ed to your computer and use it in GitHub Desktop.
Ruby notes 1

Ruby Notes - 1

  • Use is_a?, kind_of?, instance_of? method to check type of any variable

  • Explain the usage of method in detail, as a comments before method

  • Use compact method to remove nil from arrays

  • Use tap method as follows, avoid writing an extra line to return object Instead of,

    def update_params(params)
      params[:foo] = 'bar'
      params
    end

    Use,

    def update_params(params)
      params.tap {|p| p[:foo] = 'bar' }
    end
  • Instead of if condition? do_something; end, use return unless condition; do_something;. This is cleaner specifically in longer methods.

  • Instead of,

    if a.present?
      a[key] = value
    else
      a = {}
      a[key] = value
    end

    use this,

    a ||= {}
    a[key] = value
  • Always use 'str_value' instead of "str_value"

  • Make constants for strings/numbers/regex which are used/called multiple times.

  • For string replace, use String#sub(50%) OR String#tr(More than 50%) which is faster then String#gsub , so if not needed we should not use gsub.

  • Should not use exceptions for control flow, instead we can put conditions. that would be 10 times faster.

  • while loop is 80% faster then Array#each_with_index when we have bigger array, but more lines of code. Following is the example,

    #Faster
    b1 = Benchmark.measure do 
      a = (0..100000).to_a
      index = 0
      while index <= 1000
        index+=1
      end
    end
    
    #Slower
    b2 = Benchmark.measure do 
      a = (0..100000).to_a
      a.each_with_index do |i,k|
      end
    end
    
    puts "While = #{b1}"
    
    puts "each_with_index = #{b2}"
  • Directly make arrays with strings OR symbols

    %i{id email username uploaded_avatar_id} # creates [:id, :email, :username, :uploaded_avatar_id]
    %w{id email username uploaded_avatar_id} # creates ["id", "email", "username", "uploaded_avatar_id"]
  • Use mass insert query with transaction when we have to create many rows, this is much faster.

    values = upload_ids.map{ |u| "(#{@post.id},#{u})" }.join(",") #Here, we need to insert multiple uploads for a same blog/message post. So, each row would have same post_id but different upload_id. upload_ids is an array here.
    
    PostUpload.transaction do #Start transaction
      PostUpload.delete_all(post_id: @post.id)
      if upload_ids.length > 0 #use insert query if we have at least one record
        PostUpload.exec_sql("INSERT INTO post_uploads (post_id, upload_id) VALUES #{values}")
      end
    end
    ```]
  • https://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/

@ParthBarot-BoTreeConsulting
Copy link
Author

Updated it as a .md file for more readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment