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:
1 2 |
>> "level".palindrome? NoMethodError: undefined method `palindrome?' for "level":String |
So, let’s add the method to the class String:
1 2 3 4 5 6 |
>> class String >> # Returns true if the string is its own reverse. ?> def palindrome? >> self == reverse >> end >> end |
This code defines the method “.palindrome?” by using something I think I neglected to explain at the end of last night. This line here:
1 |
>> self == reverse |
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:
1 2 3 4 5 6 |
>> "".blank? => true >> " ".blank? => true >> nil.blank? => true |
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:
1 2 3 4 5 6 7 8 9 10 11 |
class StaticPagesController < ApplicationController def home end def help end def about end end |
So, with our knowledge from the previous lessons we can now appreciate this:
1 |
class StaticPagesController < ApplicationController |
The StaticPagesController inherits from the ApplicationsController! It also has methods such as home, help and about which we see were defined:
1 2 3 4 5 6 7 8 |
def home end def help end def about end |
We can also create a controller explicitly using the .new method, and then examine its class hierarchy. Take a look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
>> controller = StaticPagesController.new => #<StaticPagesController:0x007fa45170eb88 @_action_has_layout=true, @_routes=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, @_request=nil, @_response=nil> >> controller.class => StaticPagesController >> controller.class.superclass => ApplicationController >> controller.class.superclass.superclass => ActionController::Base >> controller.class.superclass.superclass.superclass => ActionController::Metal >> controller.class.superclass.superclass.superclass.superclass => AbstractController::Base >> controller.class.superclass.superclass.superclass.superclass.superclass => Object >> controller.class.superclass.superclass.superclass.superclass.superclass.superclass => BasicObject >> controller.class.superclass.superclass.superclass.superclass.superclass.superclass.superclass => nil |
We can also call the controller actions, but they return nil since they are blank:
1 2 |
>> controller.home => nil |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
class User attr_accessor :name, :email def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] end def formatted_email "#{name} <#{@email}>" end end |
Since there is a lot here, we will go through it line by line.
1 |
attr_accessor :name, :email |
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.
1 2 3 4 |
def initialize(attributes = {}) @name = attributes[:name] @email = attributes[:email] end |
So, the .initialize method is the metod called when User.new is executed. This .initialize method takes the attributes argument. Below that, two instance variables are defined and the attributes given above are pulled out.
1 2 3 |
def formatted_email "#{@name} <#{@email}>" end |
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!
1 2 3 4 5 6 7 8 9 10 11 12 |
>> require './example_user' => true >> example = User.new => #<User:0x007fa450760c68 @name=nil, @email=nil> >> example.name => nil >> example.name = "Example User" => "Example User" >> example.email = "user@example.com" => "user@example.com" >> example.formatted_email => "Example User <user@example.com>" |
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:
1 |
$ rm example_user.rb |
and then commit the other changes to the main source code repository, push to Bitbucket and deploy to Heroku.
1 2 3 4 5 |
$ git status $ git commit -am "Add a full_title helper" $ git push $ bundle exec rake test $ git push 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.
I just finished this tonight! haha, I randomly stumbled upon this. Quite entertaining. I’ll definitely continue to read on as I progress. Cheers.
Hope all is going well for you!