DEV Community

Cover image for My Journey into Rails: Beginnings
David Sánchez
David Sánchez

Posted on • Edited on

My Journey into Rails: Beginnings

I'm a software engineer whose focus is mainly in web development. Since I started coding professionally my backend experience has gravitated around Python, using Django, Flask, FastAPI, etc.

I changed my employer a few months ago, and though I'm mainly a frontend developer, I'm also expected to write some backend code in order to fulfill the tickets I'm assigned to. But in the new company, we don't use Python at all, we use Ruby and I've never used Ruby before.

At the beginning, I thought that I could start reading Ruby code and since I had experience with Python it was going to make sense as I worked with it, but I was truly wrong with that. There were a bunch of syntax differences that wouldn't make sense to me at first by looking at the code and I had to search what they were supposed to do. That's clearly not a very productive way of doing things, isn't it? So that set me a challenge in order to learn Ruby & Ruby on Rails so I can be a bit more productive in the backend side of the project.

Let's get serious about learning Ruby

I needed a way to learn about Ruby's syntax first so I can be more comfortable reading the code and understanding what it does. I used two resources to do this:

Disclaimer: As I'm not 100% into backend in the new project I was able to do this while the project advances, I'm not sure this is the best approach if your time is a bit limited in the project you're currently working on.

Box receiving some papers with Ruby logos on them

  • The Well Grounded Rubyist (3rd edition): I love this book, it teaches every aspect of Ruby in a way that's pretty simple to understand and it has some examples and exercises that will help you review the concepts once you finish every chapter of the book.
  • Codecademy: I haven't used this page for years, but it was clearly a great resource that was recommended to me by a friend. I used it after reading the book above and it helped me a lot to review even more the concepts, as you have to make some exercises in order to advance on it.

After using both this resources, I was able to better understand the syntax of Ruby and navigate through the codebase of the project as everything now fits much better in my mind.

What's different with Ruby compared to Python or Node?

A person looking at a screen

Parenthesis are optional

First thing I found different between Ruby and Python or Node was the optional parenthesis to call a method (or pass messages into an object). This was of the first issues I met when I was first reading Ruby code as it was totally different from I was used to.

class Greeter
    def greet name
        puts "Hello #{name}"
    end
end

greeter = Greeter.new # Create a new object of the Greeter class
greeter.greet "David" # prints "Hello David" in console
Enter fullscreen mode Exit fullscreen mode

Flexibility when naming methods

Ruby is very flexible when it comes to naming methods. You will find a lot of "operators" that are just language conventions using method names to do certain actions. For example, the "append" operator to add a new element into an array is just a method named <<.

numbers = [1, 2, 3, 4]
numbers << 5
puts numbers # This will print 1 2 3 4 and 5 in console
Enter fullscreen mode Exit fullscreen mode

Or the setters, that are created by appending an = to the attribute name.

class Customer
    def initializer name
        @name = name
    end

    def name # This is the getter of the name attribute
        @name
    end

    def name= newName # This is the setter, look at the = in the name
        @name = newName
    end
end

customer = Customer.new "David"
puts "Customer is #{customer.name}"
customer.name = "John"
puts "Customer is #{customer.name}"
Enter fullscreen mode Exit fullscreen mode

It always returns the last line

This is something I loved when learning Rust and I'm glad something kinda similar is present in Ruby. Ruby will always return the result of the last line of the expression, unless you set a return keyword in a statement that comes before.

In the example above, you can see that in the getter method I didn't set an explicit return keyword, but calling the name method will anyways return the value of the @name variable. That method works as if I did something as:

def name
    return @name
end
Enter fullscreen mode Exit fullscreen mode

Local variables vs Instance variables vs Class variables

These kind of variables exists in all object oriented languages I've used until today, but the syntax to create them is really different to what I was used in Python.

Local variables

These are the variables that are only available in a block and cannot be accessed outside that scope. This is the normal behavior that you find in variables that are created inside a method or that are passed as arguments.

Instance variables

These are variables that are available to all the methods of the instance. Each instance may have different values for these variables. You create this type of variable by prepending an @ character to the name of the variable, such as @name in the example above.

Class variables

These are variables that are available in the class and all instances will have the same value, modifying it in a single instance means that all instances will also have the new value updated. You create this type of variable by prepending the @@ characters to the name of the variable.

class Customer
    @@types = ["Regular", "Loyal"]

    def types
        @@types
    end
end

first_customer = Customer.new
puts first_customer.types # Regular, Loyal
second_customer = Customer.new
second_customer.types << "VIP"
puts second_customer.types # Regular, Loyal & VIP
puts first_customer.types # Regular, Logal & VIP
Enter fullscreen mode Exit fullscreen mode

Instance methods vs Class methods

These type of methods also exists in all object oriented languages I've used until today, but the syntax really confused me a bit coming from a Python background.

Instance methods

These are methods that are only accesible when you create a new instance of a class. In all the examples above I created instance methods, no big deal.

Class methods

These are methods that are accesible from the class itself and you don't need an instance of the class in order to execute them. They are created by prepending the self. to the name of the method.

class Sample
    def self.hello_class
        "Hello from the class method"
    end
end

puts Sample.hello_class # Hello from the class method
Enter fullscreen mode Exit fullscreen mode

And more things...

There was some other topics that I'd like to cover here but they will make this post really long, I'll cover them later in this series in a more detailed way.

What I like about Ruby?

It looks like a very fun language to work with and I found its syntax to be elegant and easy to understand. It has a lot of useful built-in behaviors and frameworks such as Ruby on Rails makes it really easy to go from 0 to a MVP in short time.

What I dislike about Ruby?

This is not a Ruby per-se problem, but I found the Ruby on Rails learning curve to be a bit steep. There's a lot of things that in Python are totally explicit (following the "Explicit is better than implicit" principle) and are not so "explicit" in Ruby, you'll need to search in multiple files in order to understand where they are actually declared and how they work.

That's it... for now.

I've found Ruby to be a really fun to learn language and using it has been a total pleasure. I'm eager to learn new things about the language and Ruby on Rails in order to be a bit more productive in the backend side of the current project I'm currently working on and to achieve proficiency in this language.

I love teaching others in order to better learn concepts, so I will try to make more of these posts in a tutorial-like style when I learn new concepts. If you liked this post, please be assured that I will publish new ones with other concepts or thoughts about this topic.


I hope you liked this post.

Please tell me in the comments if you want to have a post about a particular aspect of Ruby or if you have any comments or feedback that could help me improve this post, and please, don't forget to react to this post, that would make me really happy.

Thank you for your time ❤️👋

Top comments (0)