Day 13: Finishing Chapter 4 (Finally!)

I’m back at it and more determined than ever. I actually find myself looking forward to my studying.

By the way, I am trying out a new plugin for code syntax highlighting: Crayon Syntax Highlighter

Let’s start where I left off, with 4.4.3: Modifying built-in classes. So, before we took the properties of the String class adn gave them to our new class (not unlike the bite of a radioactive spider.)

Now we are going to look at how to call .palindrome? by modifying the String class to allow .palindrome? as a method!

See here that it doesn’t allow it right now:

So, let’s add the method to the class String:

This code defines the method “.palindrome?” by using something I think I neglected to explain at the end of last night. This line here:

As best I understand, self will refer to the object inside the class. So, we are asking whether the self is equal to itself in reverse.

Also, this feature of Ruby that allows us to add methods to built-in classes is powerful but with great power comes great responsibility. Also, this feature was used by Rails to add the .blank method to Ruby. The .blank method is useful to prevent variables from being blank, such as a user’s name or such. See how it works here:

This example shows us that “” is blank, but so is ” ” even though it is empty. Nil is also blank.

Next, we will discuss a controller class. We discussed classes before when we looked at the Static Pages controller. Remember:

So, with our knowledge from the previous lessons we can now appreciate this:

The StaticPagesController inherits from the ApplicationsController! It also has methods such as home, help and about which we see were defined:

We can also create a controller explicitly using the .new method, and then examine its class hierarchy. Take a look:

We can also call the controller actions, but they return nil since they are blank:

Now we are at the last stop on our tour of Ruby: the user class.

So, this time instead of entering the class definitions in the console as we have been doing all lesson, we will be creating a file in the application root directory called: examples_user.rb

Since there is a lot here, we will go through it line by line.

So this creates attribute accessors that correspond to a user’s name and email address. It creates “getter” and “setter” methods allowing us to retrieve (get) and assign (set) @name and @email instance variables. These always begin with an @ sign and are nil when undefined.

So, the .initialize method is the metod called when is executed. This .initialize method takes the attributes argument. Below that, two instance variables are defined and the attributes given above are pulled out.

Finally, a formatted_email method is defined using the values of the assigned @name and @email variables to build a formatted version of the user’s email address using string interpolation. Since @name and @email are instance variables, they are automatically available in the formatted_email method. Now, let’s load it in the console!

So, a couple of conventions new to me. The ‘.’ is Unix for “current directory” and telling Ruby ‘./example_user’ tells it to look or that file in that location. The code following creates an empty example user, fills in the name and email address to the corresponding attributes and then formats them pretty with the .formatted_email method!

And then we need to delete the example_user.rb file we made since we won’t need it anymore:

and then commit the other changes to the main source code repository, push to Bitbucket and deploy to Heroku.

Oh my. I completed Chapter 4!

So, tomorrow I will be going to my first Ruby on Rails Meetup with B’More on Rails. I will write about the experience afterwards.

2 thoughts on “Day 13: Finishing Chapter 4 (Finally!)

  1. I just finished this tonight! haha, I randomly stumbled upon this. Quite entertaining. I’ll definitely continue to read on as I progress. Cheers.

Comments are closed.