You can add an enum class to a Ruby method or object by using the Ruby Enumerable mixin.
This mixin provides a lot of useful methods for working with enums, including the enum_for method.
Here's an example:
require "enumerable"
class EnumeratorTest
include Enumerable
def each
yield 1
yield 2
…
Ruby Thread is a way to run code in parallel with other code. They can be useful for doing things like fetching data from a web service and processing it while the user is still interacting with the UI.
They can also help you make use of all the processors in your computer for things like…
If you're using gemfiles to manage dependencies in Ruby, then this article is for you. You'll learn how to implement these gemfile tips and make your project more efficient.
Stop Repeating Yourself (DRY Principle)
If you're like me, then you probably have a lot of dependencies in your Gemfile. It's a pain to keep typing the…
If you're working with Ruby and RSpec and you want to improve your workflow and efficiency, here are some Rspec tips for you.
1. Use Factory Girl to Create test Data
Factory Girl is a gem that allows you to create test data easily.
You can use Factory Girl to create objects in your tests without having…
Begin and Rescue keywords in Ruby allow to execution of some code if an error occurs during the program's implementation.
Rescuing Standard Errors
The rescued code will execute only if a StandardError or its descendants occur.
For example:
begin
some_undefined_method
some_undefined_variable
rescue
puts "An error occurred."
end
It's also possible to handle different speeches of errors:
begin
…
Ruby Classes are first-class objects. This means that a class in Ruby can have its own methods and variables (static or class variables). Classes can also inherit from other classes.
Classes in Ruby are defined using the class keyword.
class MyClass
end
The name of the class should start with a capital letter. By convention, class…
Validations are a really important concept in web development, they help us ensure that the data our users submit is valid. Rails validations provides a really neat way to define validations using declarative syntax.
class User < ApplicationRecord
validates :name, presence: true
end
This validation will ensure that the name attribute is present (not…
You can use Rails scopes to make your code more expressive.
Scopes are a way of grouping related methods together, and they are defined in your model classes.
You can use scopes to make your code more expressive and easier to read.
Example
Let's say you have a User model with a first_name and a last_name attribute.
You want…
Design patterns in Ruby (as well as in the rest of the languages ) are a standard solution to a common problem in software design. They are not specific to any programming language and can be implemented in many different ways.
There are many different design patterns, but some of the most common ones…
The Ruby's do/end keywords pair is used to create a block of code.
A block is a bit like a method, it's a piece of code that can be executed, but it doesn't have a name.
Here's an example:
5.times do
puts "Hello!"
end
# Hello!
# Hello!
# Hello!
# Hello!
# Hello!
do/end is usually used when a method expects a block…