Installing Ruby - Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Learn Ruby The Beginner Guide An Introduction to Ruby Programming (2015)

Installing Ruby

Most readers will have already installed Ruby. Along with Ruby, you'll want a text editor. My personal preference is the Komodo IDE. You can download a free trial here: http://bit.ly/komodo-ide. To minimize the verbosity of the book, I've opted to link to my Ruby installation and text editor video rather than writing a detailed explanation of installation. I will walk you through all the steps there. I recommend taking a look either way. You'll hear my voice and know what I sound like. I don't sound like a robot sadly.

Video Link: http://bit.ly/wild-ruby-install

Windows users can download Ruby here: http://bit.ly/wild-ruby-installer

Most Linux distributions have Ruby installed. If you would like to check on Ubuntu:

Open your terminal. Applications > Accessories > Terminal (This could be shell or bash shell)

Run the command: which ruby

Look for the path /usr/bin/ruby

If you see the path, Ruby is installed. If there's no response or an error, you need to install Ruby. Steps to install Ruby on Linux can be found here: http://bit.ly/wild-ruby-linux-install

Macs already have Ruby pre-installed.

Ready to Ruby

"Computers don't complain about complexity." -Yukihiro Matsumoto (Chief Designer of Ruby)

Let's get started. In your editor or Ruby shell type the following:

puts "Matz"

Make sure to include the quotation marks around Matz. Now press Enter if you're in a shell or save the file and run it. (If you're using Komodo IDE, press F7) You should see this returned:

Matz

You've now been introduced to your first data type in Ruby. Text between quotations is what's referred to as a string. A string is a wonderful thing. Let's create another:

puts "I get my own line."

print "This is a string."

print 'I am on the same line.'

I get my own line.

This is a string.I am on the same line.

Notice that we used both print and puts. Puts adds a new line after executing the command. As you might've guessed, print doesn't create a new line after. You'll also see that we used single quotations on the last string. You can use single or double quotations when writing a string. However, there are some things to be aware of. Consider this string:

puts 'This doesn't feel correct.'

Can you see why our string wouldn't feel correct? If not, try to think about where our string begins and ends. Because we've decided to use single quotes, Ruby thinks we ended our string with the apostrophe in the word doesn't. We can fix this by simply using double quotes.

print "This doesn't feel incorrect."

This doesn't feel incorrect.

Great! We avoided a syntax disaster. Let's say we wanted to use single quotes anyways. We can do it by adding a backslash before the apostrophe.

print 'You\'re learning a lot already.'

You're learning a lot already.

That's pretty nifty. Let's see what happens when we apply this logic to double quotes:

puts "Here's a quote in a string: \"Absence of evidence is not evidence of absence.\" -Carl Sagan"

Here's a quote in a string: "Absence of evidence is not evidence of absence." -Carl Sagan

These backslashes followed by apostrophes and double quotations are what's known as escape sequences.

The point of this chapter is to merely get your feet wet. We will cover more things in detail later. For now, let's try using some Ruby methods.

puts "Reverse this string.".reverse

.gnirts siht esreveR

The reverse method will put our string out in reverse. That's nifty. We should try another method.

puts "Characters in this string?".length

26

Notice that Ruby includes spaces in the character count.

That's enough teasers for now. We must move on. Take this dagger. You'll need it as we cut deeper into this.

Object Types

I need to familiarize you with a few terms before we progress further. Don't worry about understanding these in-depth right now. I will talk in detail about each one as the book progresses.

Strings: Text inside quotation marks: "I am a string."

Integer: Whole numbers: 24

Float: Numbers with decimals: 3.245

Booleans: Values that are true or false: 2 > 5 = false

Arrays: Arrays are lists of objects. (Numbers, Strings, Floats, Booleans, etc.) Each object in an array is referred to as an element. Our example is an array of numbers: [23,48,34,12]

Hashes: Hashes are similar to arrays but with Key and Value pairs: ["Tim" => 9, "Joe" => 8, "Sue" => 11]