DEV Community

loading...
Cover image for 6 concepts to know in Ruby

6 concepts to know in Ruby

jmau111 profile image Julien Maury Originally published at blog.julien-maury.dev ・3 min read

Ruby is one of the most elegant and powerful language I know.

Ruby vs. Ruby on Rails

Know the difference. It's simple, Ruby on Rails (RoR) is an open-source Ruby-based framework.

RoR is interesting because its creators and maintainers assume there are better ways than others and act in consequence. They call it "Convention Over Configuration".

No compilation is required, unlike Ruby itself, and you write significantly less code than with other frameworks.

Give it a try. It has most of the Ruby concepts we'll see here.

Metaprogramming

A program can write other programs while running. It's called metaprogramming. Nothing like the Matrix, hopefully 🤞.

Ruby developers use this ability to generate methods on the fly at runtime and keep the code DRY (do not repeat yourself).

In other words, it means you can use methods that neither Ruby nor you define. What?!!

For example, work with a database and have some tables with specific columns to query. You can do it dynamically instead of defining multiple methods to query some particular fields.

By working with define_method() or implementing the method_missing construct, you can achieve that.

But let's keep it basic here. For example, Rails dynamically creates methods based on the column names:

Articles.find_by_title("6 concepts to know in Ruby");
Enter fullscreen mode Exit fullscreen mode

Source: Ruby on Rails - Dynamic finders

Should you use that for everything and anything? Of course not, but it's a powerful feature.

Inheritance chain

Every class in Ruby has a parent class by default. At the top of the chain, you have a blank class called BasicObject. All classes inherit from it, including the Object class you can use to create new objects.

Source: Ruby documentation - BasicObject

It means that many things happen behind the scene when you call an object's method. Ruby processes the whole chain of the object's ancestors to know where this method is defined.

If the current object does not define the method, it looks into the parent, and so on. You often read the term "superclass of" instead of "parent class of", but that's the same thing.

Composition and mixins

While the class hierarchy is excellent, modules provide a way to group common behaviors.

Instead of inheriting from another class, you mix modules in your class:

module MyModule
  def myMethod
    puts "Mix it."
  end
end

class MyClass
  include MyModule
end

test = MyClass.new
test.myMethod 
# "Mix it."
Enter fullscreen mode Exit fullscreen mode

You can mix multiple modules. It helps to add various behaviors to a class while keeping the responsibilities separated.

Blocks

If you come from another programming language, it's one of the Ruby versions of closures and a pretty cool way to reuse code without constantly changing the implementation.

N.B.: Ruby has other structures that are very close to closures, such as proc and lambdas, but we won't see them here.

Blocks help overstep the normal scope's boundaries as they inherit the context and references (local variables) where you create them.

They are code blocks you can capture to execute them later in your methods, which allows for very clean organizing.

Let's take a basic example:

def notice(notice)
  yield(notice)
end

notice("Ruby") { |yahoo| puts "Great success with #{yahoo}" }
# Great success with Ruby
Enter fullscreen mode Exit fullscreen mode

N.B.: I use yahoo here because I don't want you to think you have to use the same name notice.

The yield keyword allows for executing our block.

There are way more things to say about blocks, but let's keep it basic here and focus on the purpose.

Blocks are an excellent way to encapsulate behaviors and pass them to methods to be executed later.

Nothing is an Object

nil is an instance of the NilClass class, a unique Ruby object representing an empty value. Its value is also falsy, meaning you can use it in a condition.

Only false and nil are falsy in Ruby. In a boolean context, you should consider everything else true.

Wrap up

Ruby loves conventions and makes you write reusable code.

Discussion (0)

pic
Editor guide