Ruby is an interpreted language
What does this mean? Why does it matter?
Computers process binary code, a system in which everything is represented in 1’s and 0’s. For example, the number 67 would be represented as 01000011. Each letter, number, and character key is represented by a series of 0's and 1's. So, the programs that we write are not actually the programs the computer will read. There is an intermediary, the Ruby compiler. This is the program that reads our code, checks for semantic correctness, and translates it to instructions the computer can understand. We write Ruby code, which is then interpreted by the compiler before execution.
There are two ways to execute Ruby programs.
- Execute the program from the command line
Let's create a Ruby program that says, "Hello periwinkle".
$ touch hello.rb $ nano hello.rb
From the command line create a file with the '.rb' extension. Using your favorite text editor, write a line of Ruby code that will print the words "Hello periwinkle" to the screen.
puts "Hello periwinkle"
Save the file. You can then instruct the interpereter to run the file with...
$ ruby hello.rb
(Note: the '$' indicates that I'm typing into my terminal window. It should not by used in executing the previous commands)
If everything works correctly the result of the programs execution should be "Hello periwinkle" displayed in the terminal.
- Execute from within IRB
IRB stands for Interactive Ruby. It is a command line interface for writing and executing Ruby code with an immediate response. IRB provides an excellent environment for practicing, testing, and experimenting with Ruby code. You can enter the interactive console by typing 'irb' into the command line and pressing enter. Once inside, try executing some simple arithmatic.
5 + 5 10 4 * 4 16 100 / 50 2
A language of options
Ruby is a language of options. There are multiple ways to accomplish the same task. In this respect, Ruby is a flexible language that consistently attempts to enable programmers rather than restrict them. Consider printing to the screen; You can print text to the screen using the keywords "p", "puts", or "print". Puts is short for put string while "print" is a common command across many languages. The difference between puts and print is that puts will print the screen and return a new line. The "p" functions just like puts but with an additional string inspection. There may occasionally be extra data included when using "p". As you'll see in the example below all three act differently.
'hello' hello nil print 'hi there' hi there nil p 'hey' "hey" "hey" # 3 separate options for printing to the screenputs
What's that 'nil' ?
Ruby always returns an object. It's part of the orientation of the language. The commands 'puts' and 'print' both print text to the screen. There is nothing to return so ruby returns 'nil'. This is simply the default behavior of ruby, don't overthink it. However, notice that using 'puts' prints both 'hello' and 'nil' on separate lines. Using 'print' prints both 'hi there' and 'nil' on the same line. This new line character is the difference between 'puts' and 'print'. So what is "p" doing? It prints the string, provides a new line, but then instead of returning nil it returns the string. This may seem like odd behavior at first but as you learn more it will begin to make sense. You can exit IRB by typing, 'exit' or 'quit' and pressing enter. There is also a keyboard shortcut, which is control + D.
Your output might look slightly different than mine. I prefer a clean working environment, so I have some default settings in my '.irbrc' file. If you want your IRB sessions to use the simple prompt by default you can create an '.irbrc' file in your home directory with the following setting:
IRB.conf[:PROMPT_MODE] = :SIMPLE
(Note: See the ' . ' in front of the file name, '.irbrc', this indicates that it is a hidden file.) You most likely won't be able to see the file in your home directory unless you opt to "show hidden files". This doesn't mean you can't create, edit, or delete it. From the command line, create the file and add the simple prompt configuration with this echo command:
$ echo IRB.conf[:PROMPT_MODE] = :SIMPLE >> .irbrc