Essential Ruby on Rails Methods
Understanding the Power of Small Methods in Rails
1..dup Vs .deep_dup
2..tap
3..insert_all and .upsert_all
4..dig
5..merge and .deep_merge
6..compact and .compact_blank
7..try 8..content_tag
9..delete_if and .keep_if 10. .tally 11. .methods
.dup
Purpose: Creates a shallow copy of an object.
Use Case: When you need a copy but changes to nested objects should affect the original.
.deep_dup
Purpose: Creates a deep copy of an object.
Use Case: When you need a complete, independent copy.
Purpose: Yields the object to a block, and then returns the object.
.insert_all
Purpose: Inserts multiple records into the database efficiently.
Use Case: Bulk insert without callbacks.
.upsert_all
Purpose: Inserts or updates multiple records. Use Case: Bulk insert with conflict handling.
Purpose: Safely navigates nested hashes and arrays.
Use Case: Avoids nil errors when accessing deeply nested data.
.merge Purpose: Combines two hashes, with the second hash overwriting the first.
Use Case: Simple hash merging.
.deep_merge
Purpose: Recursively merges two hashes.
Use Case: Merging nested hashes.
.compact
Purpose: Removes nil elements from an array or hash.
Use Case: Cleaning arrays or hash.
.compact_blank
Purpose: Removes nil and blank elements.
Use Case: Cleaning arrays and hashes.
Outcome: @user_ids will contain only nonnil user IDs, ensuring no errors during the query. If params[:user_ids] is [1, nil, 2, nil, 3], @user_ids will be [1, 2, 3].
Outcome: @user_ids will contain only nonblank, non-nil user IDs, ensuring clean data for the query. If params[:user_ids] is [1, "", 2, nil, " ", 3], @user_ids will be [1, 2, 3].
Purpose: Calls a method on an object if it is not nil.
Use Case: Avoiding NoMethodError.
Outcome: @profile_picture is assigned the user's profile picture URL if it exists, otherwise, it remains nil without raising an error. If @user.profile or @user.profile.picture_url is nil, @profile_picture will be nil.
.content_tag
Purpose: Creates HTML tags in views.
Use Case: Simplifies dynamic HTML generation.
Outcome: Generates the following HTML.
.delete_if
Purpose: Deletes elements from an array/hash if the block evaluates to true.
Use Case: Filtering elements.
.keep_if
Purpose: Keeps elements from an array/hash if the block evaluates to true.
Use Case: Filtering elements.
.tally
Purpose: Counts occurrences of each element in an array, returning a hash.
Use Case: Counting frequencies.
Outcome: @product_counts will be a hash with product IDs as keys and their count as values. If product_ids is [1, 2, 1, 3, 2, 1], @product_counts will be {1 => 3, 2 => 2, 3 => 1}.
.methods
Purpose: Lists all methods available on an object.
Use Case: Debugging and exploring available methods.
Outcome: @user_methods will contain a sorted array of all methods available on a new User object. This helps in debugging and understanding what methods can be called on the object.
Summary:
1. Recap the importance of these methods.
2. Emphasize how they make code more efficient and readable.
3. Encourage practice and exploration of these methods.
Bluebash (India)
A-45, Industrial Area, Phase -8B,Sector
74, Sahibzada Singh Nagar, Punjab 160059, INDIA
Contact Number +91-82848 66471
Email – Sales@BlueBash.co
Bluebash (USA)
30 N. Gould St. Ste R, Sheridan, WY 82801, USA
Contact Number +1(307) 438-9484
Email – Sales@BlueBash.co
Bluebash (Canada)
6th Floor, 8 Nelson St. W, Brampton, ON, Canada, L6X 1B7
Contact Number +1(307) 438-9484
Email – Sales@BlueBash.co
www.bluebash.co