Day 11 Part 3: Return of the Rails

OK, back from playing board games with my wife and eating a crab cake. The crab cake was good and I am convinced my wife cheats at board games.

Back to the tutorial. So, nil is an object. As such this can be done:

Where the method to_s mto convert “nil” into a string. You can also try to ask whether nil is empty:

Unfortunately that returns an error. You can, however, use message chaining to get the response desired:

You can also use Ruby to answer a seemingly philosophical inquiry:

So neither “foo” nor “” is nil. Only nil is nil. That is deep.

Earlier we had defined “x” as “foo.” The following statement can be used to evaluate whether the string “x” is empty:

This is an alternate way to use the “if” keyword. You can also use “unless” the same way:

Going back to “nil,” it is a special object. It is the only Ruby object that is false in a boolean context. Here is a demonstration by using the !! (bang bang) which negates an object twice, coercing it to its boolean value.

By the way, the last was not something in the tutorial. I thought I would try it. I had to look up the error. According to an answer on Stackoverflow, the warning is because a string literal will always be true. Interesting.

Next up is method definitions. We saw this back when we used it to make our first two apps. We can also do the “def” command in a console. Take a look as we define a function:

An interesting note in the tutorial is that the variable “str” could be replaced with any valid variable name. He used the_function_argument and it worked. I decided to try something else:

So, at this point Mr. Hartl says we are able to understand the entire full_title helper from earlier in the tutorial.

The one element left is the topmost one:

Modules are a way to package together related methods. The Ruby documentation says:

A Module is a collection of methods and constants.

OK, it is 1AM and as much as I want to continue and finish Chapter 4, I should sleep. It is taking me longer, but there is a lot of meat in this chapter. I will continue bright and early tomorrow with “4.3 Other data structures.”