Skip to content Skip to footer

All Posts

Exploring The Ruby Enumerable Module
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 …
How to Use The Begin and Rescue Keywords in Ruby
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 …
An Introduction to Ruby Classes
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…
Secure Your System With Rails Validations
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…
How to use Ruby’s do/end keywords
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…