Introduction
4 weeks ago I kicked off a new journey of learning Ruby, a backend programming language. Coming from Javascript language, I find Ruby interesting and easy to learn. And as a result, I've decided to document my experience with Ruby by sharing what I have learned, starting from Ruby basics to more complex concepts.
What is Ruby?
Ruby is a dynamic object-oriented, general-purpose programming language - Definition from Ruby documentation.
Ruby was designed to make programmers happy. It's simple to use but capable of designing complex things.
Everything in Ruby is an object.
Enough of theory part, let's get into our code editor and start coding.
VS Code extensions for Ruby
In this series I will be using Visual Studio Code editor. To get started install these extensions:
Ruby - For Ruby language support and debugging
Ruby Solargraph - Provides code completion, intellisense.
Creating your first Ruby Application
In your VSCode create a folder and name it as you wish. Inside that folder create a file with .rb
extension Eg hello.rb
You will be using the .rb (stands for Ruby) extension when creating your Ruby files.
For now don't worry about the Ruby project file structure, we will be discussing it in another post.
Ruby Comments
Comments are line of code that are not executed or are ignored by the interpreter. Its a good practice to add comments into your code for better understandability and readability.
Single line Comment
Write this line in your hello.rb file
#My first Ruby App
starting with the hash(#) sign.
For single line comments, you use a # in the beginning of the line. The sentence that follows the # is not executed.
Multi-line Comment
You may want to comment out several lines of code in your file. You might think of adding the the # sign in each of those lines but this will be tiresome and repetitive.
So how do you do this?
=begin
several lines of code goes here
several lines of code goes here
several lines of code goes here
=end
Every line of code between the =begin
and =end
- don't forget equal sign(=), is regarded as a comment hence not executed.
In your hello.rb file, try out the multi-line comment by adding several lines of code and enclosing them between =begin and =end
Displaying Ruby Output(String) - puts and Print
If you are coming from Javascript language, do you remember what console.log
method does? If you answered displaying output to the terminal, then you are right.
In Ruby we use puts
or print
methods to output string output to the terminal.
Add this code into your file:
puts "Hello World!"
puts "My name is Cate"
print "Hello World!"
print "My name is Cate"
Difference between puts and Print
Puts adds a new line break after the output while print does not.
To see this difference, let's run our Ruby application to see results in the terminal.
Running Ruby Application
To run Ruby code in the terminal we use this format : ruby filename.rb
whereby the filename.rb is the name of file you want to run. In our case, we named ours as hello.rb
if you have been following along.
In your terminal write this command:
ruby hello.rb
And hit Enter.
What do you see? Do you see the difference between the puts output and that of print?
puts output
Hello World!
My name is Cate
print output
Hello World!My name is Cate
You see the difference now. puts adds a new line between its output while print displays the output as one sentence.
If you want to see your output nicely and clearly use the puts method.
Congratulations!!! You just run your first Ruby app.
Let's talk about two other Ruby's output methods **p**
and **pp**
.
Outputting complex data using p
and pp
Methods
We have seen how to display string output in the terminal using puts and print methods.
But when it comes to more complex data like arrays and nested hashes, puts and print might not be the best way to inspect that data.
Don't worry about array and hashes concept if you are new in programming. We will be going through them later.
Outputting array data using p
p method calls the .inspect method on our data and output it in a nice format.
p [10, 20, 30, 40]
**Output**
[10, 20, 30, 40]
Outputting nested hashes data using pp
pp stands for Pretty-printing and is used to pretty-inspect our complex nested data and output it in a nice readable format.
pp [{name: "cate", age: 22},{name: "Kyle", age: 30},{name: "Joy", age: 25}]
**Output**
{:name=>"cate", :age=>22},
{:name=>"Kyle", :age=>30},
{:name=>"Joy", :age=>25}
Conclusion
That's enough for today. Practice working with puts, print, p, and pp methods as well as running your Ruby application in the terminal to see the resulting output.
Happy Coding!
Top comments (2)
Good start. I would say that it is not standard practice to use multiline comments the way you describe:
Rubocop Rule on Multiline Comments
Installing Rubocop is an excellent guide in your learning, as it will hook into Solargraph and correct these errors. In Ruby, there are a number of ways to do the same thing, but the community (and big style guides) have decided on best practices. It's good to learn those at the same time as you learn Ruby.
It's not necessary to learn every way to do something in Ruby - just learn the standard way of doing things.
Thank you for correcting me on that. I will definitely check the Rubocop rules on multi line comments.