4. Your First Ruby Program

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.

image.png

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"

image.png

"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!

image.png

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:

image.png

Notice how every line is printed on a new line. Remember with puts, it creates a new line after each output.

Error Messages

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:

image.png

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)

With 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.

Comments

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.

Conclusion

  1. Ruby files are saved with a .rb extension
  2. puts is a command that prints out a string with a line break
  3. Strings are a set of characters, that are declared inside " "s or ' 's
  4. You can run a Ruby program with the ruby command
  5. Error messages tell us what we are doing wrong
  6. As a programmer, you will face many error messages every day - the key is to be calm and tackle the problem

Coding Challenge