Beginner Series: Ruby Tutorial
First, we need to open a terminal or command prompt so that we can work with the code. After you have installed Ruby, open up the IRb shell.
- If you’re using Mac OS X you can do this by opening up the Terminal typing “irb”, then pressing enter.
- If you’re using Linux, then open up a shell, type “irb”, and press enter.
- If you’re using Windows, you open Interactive Ruby from the Ruby section of your Start Menu OR you can open a command prompt, type “irb” and then press enter.
Hello World
As you probably already know, one of the first things you will learn to do with any language is how to output the text, “hello world.” We’re going to do the same thing here, but be sure to take note of how easy that is to do with Ruby.
After you’ve opened the IRb shell, just type the following into the prompt:
puts "Hello World"
When you press enter, the terminal should return the following:
Hello World => nil
The “Hello World” part is obviously the text that we asked the shell to output. The “nil” is the result of the expression, or the shell telling us that the final value is absolutely nothing.
If you’re wondering whether or not that means we can use the shell and Ruby code as a basic calculator, then the answer is yes.
Type any simple equation into the terminal, and press enter to see the result. For example:
irb(main):003:0> 7+2 => 9
Congratulations! You know how to work with the Ruby shell now!
Defining Methods
In Ruby, we can define methods to make things a little simpler. This is like a basic function, where we give the method a name – which will be used to call it – and then give it something to do.
For example, let’s start with this:
def method puts "Yay! This is our method!" end
In the code snippet above, we are defining a method called “method,” which will output the message “yay, this is our method,” after which it will end.
Want to know how to activate the method? It’s simple.
Type the name of your defined method like so:
method Yay! This is our method! => null
That was pretty easy, right?
Let’s create a new method that asks the user for some personal information! In this way, we can make our final program (which is what the compiled code is) interactive.
You start by defining a method, and then asking it to prompt the user, like so:
def hello puts "Hello #{name}! Have a good day!" end
When you call your method using this snippet, it will pause and ask you to input a name. Go ahead and enter any name you want in place of Bob’s. It should look something like the following when you run the code.
hello ("Bob") Hello Bob! Have a good day! => nil
Notice how we also defined the name that would be used by our method in parenthesis? You will need to do this, as well.
If you see the same output as the one above – with your name instead of Bob’s – then you’ve done it right! Congratulations, again! You’re one step closer to becoming a Ruby pro.
Working with Classes and Creating an Object
Last, but not least, we’re going to learn how to work with classes and objects to store information. We’re going to do this by creating a simple AI program that will greet us by name and tell us farewell when it’s done.
First, we’re going to create the class that will include the methods (instructions) for our AI. The code to do that is the following:
class Greeter def initialize(name = "AI") @name = name end def hello puts "Hey #{@name}!" end def goodbye puts "Bye #{@name}, have a good day! Come back and talk soon." end end => nil
Our class is called “Greeter” and below that are our methods that will take place when we activate that class.
To make this work the way we want, however, we need to create an object called “Greeter” which is the same name as our class.
greeter = Greeter.new("Bob") => #<Greeter:0x16cac @name="Bob">
In this object we are telling the AI our name is “Bob” so if you want to use a different name swap out Bob’s (obviously).
Now our object will remember the name we entered.
Let’s give our program a try!
greeter.hello Hey Bob! => nil
Works great! Now let’s make it say goodbye!
greeter.goodbye Bye Bob, have a good day! Come back and talk soon. => nil
Sweet! We’re getting good at this, huh?
To wrap things up, we put together a list of resources that you can use to further your Ruby training.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.