Chapter 1: Part 2

There are some formulations of strings and numbers don't interact well.

puts 'Mulder' * 'Scully'
puts 8 - '2'
puts 'Fab' + 4
puts '6' * '7'

To better understand why some of these mixed type lines of code work and some don't, we need to look inside of Ruby to see what is happening. In the first example, we are trying to take the string 'Mulder' and multiply it 'Scully' number of times. This is patently ridiculous so it's no surprise that Ruby can't make heads or tales of it. In the third example, we see that though different data types can work together, they cannot be combined. The second example is a little trickier. Here we are taking the number 8 and trying to subtract from it the digit '2'. In all programming languages it is important to understand this difference.

puts 'Ziggy piggy!' * 4

As you recall, in Ruby everything is treated as an object. So when you use an operator like * with the operands 'Ziggy piggy!' and 4, Ruby reads it as "print four of this object to the screen". It doesn't really matter what that object is. In cases where the object is a number like 6, the line 6 * 4 means "print four of the number 6 to the screen" which is equivalent to printing 6 + 6 + 6 + 6, or 24. Ruby evaluates code from left to right, one line at a time so the order of the operands matters greatly. Compare this code to the above example:

puts 4 * 'Ziggy piggy!'

Ruby can't multiply things by strings so this order doesn't work. By making everything an object, there are less restrictions in the configuration of Ruby. In fact, there is a well-known Ruby mantra: "Choose Convention Over Configuration". The language itself does not require very much regarding typing and naming of variabels. By not requiring strong data typing for variables, the developer has more freedom for creativity. But if you choose to follow the many well-established Ruby conventions, your code can be just as powerful as more restrictive languages. As you travel further down the path to Code Englightenment, you will discover ways to use those conventions to supercharge your programs.

Of course we want to store these numbers and strings for use in our programs, and for that we assign them names, or variables. Ruby variables can be any sequence of letters, numbers, and underscores so long as the variable name begins with a lowercase letter. Because of this there is a widespread convention to use lower camelCase for all variable names. Direct your gaze to the next code examples:

var_name = 'This is a string variable'
days_since_outbreak = 28

In the first example, we have a name var_name and we use it to point at the string 'This is a string variable' while our second example sets the variable name to an integer value of 28. Now that we have these values safe and sound, we can play around with them a little starting with printing.

puts var_name
puts days_since_outbreak

Will output This is a string variable and 28 respectively.

puts var_name * 2

Will output This is a string variableThis is a string variable with no spaces since we didn't add any in there. The same basic rules of string arithmetic apply here as well.

days_before_rescue = days_since_outbreak + 31
puts days_before_rescue

In this example, we create a new variable called days_before_rescue and set it to the value of days_since_outbreak(which we just set to 28) plus 31. Then we print it and get an output of 59.

We also want the ability to display variables within strings to make using the data from our programs much easier. To do this Ruby uses the #{variable} syntax to reference variables from within strings. Take a look:

color = 'orange'
puts "My favorite color is #{color}"

The above code will output My favorite color is orange. Simple. So far we've been holding your hand. Now you know more than enough Ruby to write a fun little program.

name = 'Jon Snow'
age = 20
father = 'Ned Stark'
home = 'Winterfell'
title = 'Lord Commander'
num_of_brothers = 1
num_of_sisters = 2
puts "Let's talk about #{name}."
puts "He is the son of #{father}, former lord of #{home}."
puts "He's #{age} years old."
puts "Actually that's pretty old for Westeros."
puts "He was promoted to #{title} of the Watch last I heard. "
puts "I hope #{title} #{name} and his #{num_of_brothers + num_of_sisters} siblings get to see #{home} again."

Our program starts by creating some variables and assigning them values, then it puts strings that reference those values using the #{variable} form. This is what Ruby outputs from the program.

Let's talk about Jon Snow.
He is the son of Ned Stark, former lord of Winterfell.
He's 20 years old.
Actually that's pretty old for Westeros.
He was promoted to Lord Commander of the Watch last I heard.
I hope Lord Commander Jon Snow and his 3 siblings get to see Winterfell again.

Methods are actions we make on objects. You have seen them already. Things like puts, .length, and .capitalize are all Ruby methods. Most Ruby methods make use of the dot syntax to connect methods and objects. Methods can contain lowercase letters, underscores, and question and exclamation marks. Those last two are for special methods we will talk about next lesson.

puts 'I want to believe.'.length

This code outputs the character length of the string, which in this case is 18. Methods can sometimes be chained together like so: