Skip to content Skip to footer

All Posts

How to use the Ruby strftime method
If you want to format dates in Ruby, you can use the strftime method. The strftime method is defined by the Time class, and it accepts a string containing special format characters. For example, if you want to get the current date as a string, you can use this code: Time.now.strftime("%d %B %Y") # "02 February 2020" In the example…
How to use ActiveRecord find_by method
The ActiveRecord find_by method is a shorthand method for finding one ActiveRecord object using one of the model's attributes. What is ActiveRecord find_by Used For? The find_by method queries your database for a specific record or records that match certain criteria. How to Use ActiveRecord find_by The basic syntax for using find by is this: Model.find_by(attribute: value) For example,…
How to use where ActiveRecord method
The where ActiveRecord method is one of the most powerful features this ORM offers. It allows you to express your conditions as simple key-value pairs, making your code more readable & shielded from SQL injection attacks. Here's a quick overview of how to use it: User.where(name: "John") The where method will return an array of matching…
How to install Sidekiq in Ruby
Sidekiq is a Ruby gem for background processing. It's one of the most popular background job processing tools available. In this article, you will learn how to install Sidekiq in your Ruby project & how to monitor your jobs. Installing Sidekiq If you want to use sidekiq in your Ruby project, you need to add…
How to work with hashes in Ruby
Hashes in Ruby are one of the most common data structures available. A hash works using a key-value format. Hashes in Ruby are considered objects, meaning they have specific methods you can use. Creating a hash Hash.new is a method that creates a new hash object. hash_1 = Hash.new Hashes can also be created using the literal…
How to Delete a File in Ruby
If you want to delete a file in Ruby, you can use the File#delete method. File.delete("test.txt") You can also delete a file using the FileUtils#rm method from the FileUtils module. require "fileutils" FileUtils.rm("test.txt") Deleting Multiple Files If you want to delete multiple files at once, you can use the FileUtils#rm_rf method, which is also part of the FileUtils…
How to implement HTTParty in Ruby
In this article, you will learn how to use HTTParty in Ruby to make HTTP requests from your Ruby apps. Why Use HTTParty? Gems like HTTParty & Faraday prove that making HTTP requests can be fun & easy in Ruby. You can easily install HTTParty to your Gemfile: gem 'httparty' And then run bundle install…