Let's write our first Ruby program!
First, make sure you are in the ruby_fundamentals
directory. You can do this by typing pwd
(print working directory).
Next, let's make a new file called hello.rb
.
As you can see, you can create a Ruby files by saving the file with a .rb
extension.
Inside the file, let's write the following lines of code:
puts "Hello world"
"Hello world"
is a String object, otherwise called a string. Strings are a set of characters, that are declared inside " "
s or ' '
s.
puts
is a Ruby command that prints out the string with a line break.
Save the file. In the command line, let's type in the following:
ruby hello.rb
You will see that Hello world
is printed to the terminal!
As you can see above, we can run ruby programs with the ruby
command.
Let's print out some more code. Inside hello.rb
, let's add the following lines of code:
puts "I'm learning Ruby right now"
puts "This is easier than I thought"
puts "What a wonderful day"
Save the file. Let's run the program. Type in the following code in the command line:
ruby hello.rb
Your output should now look like this:
Notice how every line is printed on a new line. Remember with puts
, it creates a new line after each output.
Let's add a new line of code to our hello.rb
file:
put "Today's an awesome day for coding!"
Change your file to look like this:
puts "Hello world"
put "Today's an awesome day for coding!"
Save the file and refresh the page. You will see an error message:
As a programmer, you will see a lot of error messages. The important thing is to not freak out, stay calm, and solve the problem.
When the program reaches an error message, the program will stop at the location where it errored.
Error messages tell us a lot about what we are doing wrong in the program. Let's break down this error message:
hello.rb:2:in `<main>': undefined method `put' for main:Object (NoMethodError)
hello.rb:2
- this is telling us that the error is on line 2 of the hello.rb
fileput
since it is undefinedWith this information, we can start debugging the program. The bug here is that put
isn't a command in Ruby - instead it should be puts
.
puts "Hello world"
puts "Today's an awesome day for coding!"
Save the file and run the program again. You will see that the error message has gone away.
Debugging is a skill in itself. Even experienced developers face many bugs every day, and it is part of being a developer. The more bugs you debug, the faster you will be at debugging errors.
As a programmer, often times we want to leave notes for other programmers to read, or to leave a memo for yourself.
Change hello.rb
to look like this:
# this is a comment
puts "Hello world"
# text after hash tags cannot be seen
puts "Today's an awesome day for coding!"
Save the file and run the program. You will see that the program will not print out the text after the hash tags.
.rb
extensionputs
is a command that prints out a string with a line break" "
s or ' '
sruby
command"I'm ready to become a ruby developer!"
to the console.