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…
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,…
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…
In this tutorial, we will learn how to install Ruby with rbenv.
rbenv is a tool that helps you manage & switch between different ruby versions. It is similar to Virtualenv for python or RVM for Ruby. It's handy because it's easy to use, it's fast, & it's cross-platform (Linux, macOS, & Windows).
It is useful when…
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…
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…
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…
There are a few different ways to remove the first position of an array in Ruby, but the most common is to use the Array#shift method.
This method will remove the first element from an array & return it.
numbers = [5,3,2,1]
numbers.shift
# 5
numbers
# [3,2,1]
If you don't need the element that was removed, then you can just call…
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…