DEV Community

Cover image for From Learning the Basics to Finally Writing Ruby Code
Valentine
Valentine

Posted on

From Learning the Basics to Finally Writing Ruby Code

My journey with Ruby has been an interesting transition from simply learning programming concepts to finally sitting down and writing Ruby code myself.
When I first started learning Ruby, I spent most of my time trying to understand the fundamentals: variables, methods, parameters, arguments, scope, classes, modules, and method chaining. At first, some of these concepts seemed simple when explained, but writing them myself made me realize that understanding code and actually producing working code are two different things.
Today felt like an important step forward.
I finally spent time experimenting with Ruby directly inside IRB (Interactive Ruby). Instead of only reading examples, I started defining my own methods, calling them, making mistakes, changing the code, and observing how Ruby behaved.
One of the methods I experimented with was a simple cube function:
def cube(num)
num * num * num
end

puts cube(3)

Output:
27

It is a small piece of code, but there is a lot happening here. num is the parameter, 3 is the argument, and Ruby evaluates the expression and returns 27.
I also experimented with Ruby's return behavior:
def cube(num)
num * num * num
2
end

This returns 2, not 27, because Ruby automatically returns the last evaluated expression in a method unless we explicitly use return.
Then I tried:
def cube(num)
return num * num * num
2
end

This returns 27 because once Ruby reaches return, the method stops executing.
These experiments are helping me understand something important about learning programming: mistakes are part of the process. Sometimes changing one line and seeing an unexpected result teaches me more than simply reading the correct solution.
I'm still learning Ruby, and there is a lot more ahead, especially as I move deeper into object-oriented programming and Ruby on Rails. But I'm happy with the progress so far.
From asking "What exactly is a parameter?" and "What's the difference between a parameter and an argument?" to actually writing and testing methods in IRB β€” that's progress.
The journey continues. One method, one error, and one lesson at a time.

Top comments (1)

Collapse
 
sinarezaei profile image
Sina Rezaei •

The IRB approach is probably one of the most useful parts of learning Ruby because it exposes behavior that is easy to understand incorrectly from examples alone.

One thing that becomes interesting after implicit returns is how Ruby treats assignment as a method call. For example, with attr_accessor :name, name = "Ruby" and self.name = "Ruby" are not equivalent inside an instance method. The first creates or assigns a local variable, while the second calls the generated name= method.

That kind of behavior is where Ruby starts feeling very different from languages where assignment is treated purely as an operator.

I think experimenting with these small language rules in IRB is a much better way to build a mental model than just memorizing syntax. Once you move into Rails, these details start showing up everywhere.