Ruby: Math and Output

Testing Ruby code using IRB

Before we start actually creating any Ruby files containing code, let's just get to know the language a little. One of the easiest ways to do this is to use something called IRB, which stands for Interactive Ruby. Open up your terminal and enter the command irb (or just open 'fxri' if you're using Windows) - if Ruby is installed correctly (see the previous tutorial) then you should get your version of Ruby spat out with a > sign next to it. From here, you can type in any Ruby code you like and IRB will respond by doing whatever you said and showing you what it evaluated from what you wrote.

One of the easiest things to do with basic Ruby functionality like this is basic mathematics. As you might expect, the four most basic mathematical functions are build into Ruby as operators (symbols/signs which do things):

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division

So if we write a simple mathematical expression like 5+5 into the IRB, it should evaluate the expression and tell us the answer. In this case, entering 5+5 into IRB will return 10. Go ahead any try writing some other mathematics and see if Ruby returns the correct answer (it should do!). Getting IRB to evaluate an expression is all well and good (especially for practice), but what about if we actually want to output something? The only reason that 10 was returned from 5+5 is due to the built in functionality of IRB, this wouldn't happen in an actual Ruby program. To output things in Ruby we use the puts method.

A method is a piece of code performed on an object (don't worry if you don't understand the whole object malarkey yet – it'll make sense later on) and when pieces of code like this (called either methods or functions – once again, more later) need a value to work properly (in this case the puts method needs something to output) we can pass them values via a space and then by writing the value. So if we wanted to actually output 5+5 using puts, we could write puts 5+5 - try it, it works! Alternatively, depending on how you like to do things, you can also pass these values (which are called arguments or parameters by the way) in brackets (although using a space is usually the preferred way in Ruby) – so you could use puts(5+5) instead of puts 5+5 if you wanted to.

We can also output text to the screen by passing puts a string. A string is a series of characters (letters) and these can be defined in Ruby using single or double quotes. So if we just type 'Hello World' or "Hello World" in IRB, we can see IRB evaluates the expression by spitting it straight back at us. We can output the string by simply passing it to puts, like puts 'Hello World'. While we're here, what's with the => nil part that appears when the IRB does it's evaluation of puts? Well it's quite simple really, puts always returns nil (which basically means 0 or nothing) and so the evaluation will always result in nil.

So now we know how to output basic text, let's put this to some 'real' use by creating our very first 'proper' Ruby file! Creating a Ruby file is very simple - simply open up a text editor of your choice (I personally like using Sublime Text 2), write some Ruby (in our case - we probably just want some simple code like puts 'Hello World') and then save the file with the '.rb' file extension (for example 'Hello.rb'). Once this is done simply open up your terminal, navigate to wherever the file is (in most terminals this is usually accomplished using the cd command) and then enter ruby followed by the name of the file to get Ruby to interpret it - in our case we might enter something like ruby Hello.rb. Ruby should then interpret the file as instructed and output the results (if any)!