Day 11: Continuing Chapter 4 of Ruby on Rails Tutorial

Continuing on, the next part of Chapter 4 will be using the Rails console. It can be accessed using the command:

** Note, I will be using >> for the console prompt.

First, is looking at comments. This is something touched on a few times already. In Ruby on Rails (I will call it RoR from now on to save time) you do a comment with a hashtag first, like so:

and this can be done in the console, too.

The thing to understand, however, is by using this the console will ignore everything after the #.

Next, the lesson looks at strings. Using ” ” marks, whatever you put inside is printed to the screen, so that:

Prints

and

becomes

What is cool, I think is that you can do math with these. The example in the tutorial is:

This is called “String concatenation.” You can also do something called “variable assignment” where you assign a variable a string, like so:

and then you can do this:

This is called “String interpolation.”
or even get fancier:

You can also do the math like above, but notice it just jams the name together. You then have to add a space in. The string interpolation works better.

Next, we move on to the “puts” command. Is it sad I flashed back to Silence of the Lambs? “It puts the lotion on its skin…” Anyways, moving on…

The “puts” command prints the content of the string, removing the ” ” marks and then returns “nil” back. Nil is a Ruby value for “nothing at all.” It looks like this:

There is also a “print” command, and I find it strange it also returns “nil” but it looks like this:

Why? No idea. If you type the command like this:

Then you get the “nil” on its own line. Come on Ruby, get your act together.

The tutorial then touches on a question I have actually had in the back of my mind since I started this: single quotes vs. double quotes, or ‘ vs ”

I have wondered if they were interchangeable. I guess they are not. I tried ths out when I got to this part:

I noticed specifically that last one where it took my single quote marks: ‘foo’ and made it “foo”

That to me is very interesting. Let’s look at the prinicple implied here, which is stated in the tutorial as, “There’s an important difference, though; Ruby won’t interpolate into single-quoted strings.” See this:

So, in this example you can see I assigned the first_name and last_name variables, and then I did the string interpolation using the double quote marks. It returned the expected result. I then did it using the single quote marks and returned a backslash # combo. I am still trying to get my head around the backslash lesson here from the tutorial. Look at this:

>> ‘\n’ # A literal ‘backslash n’ combination
=> “\\n”

\n is a newline command. When put inside the single quotes, it returns a double backslash and then the “n.” The tutorial states that: “As with the #{ combination in our previous example, Ruby needs to escape the backslash with an additional backslash; inside double-quoted strings, a literal backslash is represented with two backslashes.”

I feel like I am missing something here. Thankfully, I was able to ask about this at the Saturday Coding Slacker chat, and come to find out that the \ tells Ruby to pay attention to the special character following it. Single quotes are used for raw strings. Also, “Just use double quotes all the time, and you’ll be fine.” Hat tip to practicingruby for the help. I am saving his explanation here for my later benefit:

So… single quotes vs. double quotes.

Just use double quotes all the time, and you’ll be fine.

The thing about single quotes is that they’re meant to be used for raw strings, things that don’t have special control characters in them.

The advantages of using single quotes are mostly very very subtle, and irrelevant to most everyday coding

So… here’s the thing… backslash usually means “Whatever comes after this is a special character, so do something special with it”

In single quotes, you don’t have many special characters, but you still need a way to express single quotes inside of single quotes!

>> ‘For \’example\’, this string’
=> “For ‘example’, this string”

Without the slashes, Ruby wouldn’t know what to do, because you’d have this:

‘For ‘example’, this string’

Which to Ruby looks like two strings , not one, with an *example* method or variable jammed between them.

So.. that’s why you still need ‘\\’ instead of \

Because even though \ is pretty much useless in single quotes, it is necessary for escaping ‘, so now since \ has a special meaning, you need to be able to escape itself

The next part of the tutorial deals with objects and message passing. Everything in Ruby is an object. You can pass a message to an object like so:

In this example, the “.length” message inquires as to how many characters the string “foobar” is. Let’s look at another:

So, in this one I asked whether the string was empty using the message “.empty?” and found that “foobar” and ” ” where not empty, but that “” is empty. Isn’t it interesting that it recognized the space I made as not empty? Well, I think it’s interesting.

The question mark at the end is a Ruby convention that indicates the return value is boolean: true or false. This is good for control flow. Take a look at this:

So, you define s as “foobar” and then you ask Ruby to tell you whether it is empty, and give it a few strings to phrase the response. The result is “The string is nonempty.”

OK, I will return later. My Jack Russell, Ajax, needs picked up from the groomer!