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.”

Day 11, Part 2: Electric Boogaloo

OK, I’m back for a short session.

First, a picture of my dog, Ajax. This one is from my old house in Ohio. He enjoys sitting and looking out windows.

13386_1621921811384526_578219188023470348_n

Let’s continue on with the elseif command. If you remember where we left off, we did this to demonstrate control flow:

We can add in “elsif” to add more than one clause, such as:

I will admit to getting confused at this until I realized I had to leave the console because of something I did and restart it. I forgot to assign “foobar” back to “s” and I got errors. I went back, assigned “foobar” and it worked!

We can get a little deeper into Boolean variables using && for “and,” || for “or,” and ! for “not.” Take a look here:

You can see the first statement was not printed out since it was false. The others were true and it printed these to the screen.

OK, I am taking off again and will return later. My goal is to finish Chapter 4 tonight.

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!

Day 10: Working Through Chapter 4 of the Ruby on Rails Tutorial

I will be going back and typing up my reviewing of Chapter 2 and 3, but I want to get in the habit of reviewing the content on here. It really helped me to solidify what I was learning. I definitely suggest it to others!

So, Chapter 4 is titled Rails-flavored Ruby. Mr. hartl explains that, “This chapter is designed to give you a solid foundation in Rails-flavored Ruby, whether or not you have prior experience in the language.” I will be working on it tonight and tomorrow morning, so it will be split up on here. I want to absorb it, not just breeze through and not get it. Here we go!

The chapter starts out looking at the application.html.erb file in views/layouts. In particular, the focus is on this line:

The “stylesheet_link_tag” is a Rails function that works to include “application.css” for all media types. The first goal in this chapter is going to be to create a “helper” to allow for the base title, “Ruby on Rails Tutorial Sample App” to appear on the home page without the leading pipe symbol, the |. Not sure why we hate the | but down with the |.

To do this, we will be creating a helper called “full_title” that will return only the base title. The helper will be created in “app/helpers/application_helper.rb” and will look like this:

And then, we go to “app/views/layouts/application.html.erb” and switch this line:

to

and then go to “test/controllers/static_pages_controller_test.rb” and change the test for the Home page to say:

and go to the “app/views/static_pages/home.html.erb” file and remove the line:

Then, we run the Guard exec and see that we only have no failures.

Well, that is it for today. I had another long day of work and I am beat. I will be continuing in the morning, plus I plan to drop in to the Saturday Coding at CodeNewbie on Slack tomorrow. See details here. My goal for tomorrow is to complete Chapter 4 and maybe to do my review of Chapter 2. I am not sure yet if I should continue to do the reviews as thoroughly as I am or to push on quicker through the material. Any thoughts?

Day 9: A Review Day

Today was a hard day at work. I work with teen boys currently as a clinician at a group home. Teen boys will wear you out! In any case, a 10 hour day is rough. I got home late and could have said heck with it and went to bed after eating, but I am committed to doing this! Since I do have to sleep soon, I decided today would be a good review day.

I recently left a message over at StartHere.fm about my journey and received this advice from Dain Miller, one of the hosts:

Writing the blog is genius, you learn faster when you teach. Make the focus of your blog teaching what you just learned (and not just ‘i did this today and then this and then this’) and you will retain it 2x better and 2x faster than your peers.

He has a good point. I have always done well with writing stuff I am learning, so I will be reviewing the code and techniques I am learning on here. It will be boring, but I feel blogging is as much personal as it is communicative to an audience. I will be reviewing Chapter One of Michael Hartl’s Ruby on Rails Tutorial.

First and foremost, I DID install Ruby on Rails environment on my Windows 8 PC. I decided, however, to try out the free IDE on Cloud9. So far, I love it. It is really nice so far. I still don’t know about paying for it this early in my journey, however.

My install was pretty seamless on Windows 8 using RailsInstaller. The walkthrough had me up and running in a few minutes. I will hopefully try out coding in it soon. For now, however, I will stick with Cloud9.

So, the first chapter of the Ruby on Rails Tutorial involves making a “Hello, World!” program. This is done by first creating the application using the command:

*** By the way, I will be including the $ symbol as where I am typing. It is the Unix-style command line prompt. ***

The first part of this indicates the version of Rails to use to create a skeleton app, this being version 4.2.0. Next, the “new” command tells the system to actually create the directories. Finally, the “hello_app” part names the directory.

Next, we modified the Gemfile. The tutorial has us modify the existing Gemfile that was installed above with a version that installs specific versions that Mr. Hartl wishes us to use in the tutorial. This was done by a simple copy and paste. Next, however, comes the command to install the gems. This is done first by ensuring in the command line that we are in the right folder using:

and then using the command:

This installs the gems specified in the gemfile. From what I understand, the command above to install the new application did this before, but after modifying the gemfile to specify the exact versions we wanted, we run it again to install these.

Next is starting the Rails server. This is done using the following command:

If you were on a system running on a local machine, “rails server” is all that is needed to start the server. The “-b $IP” and “-p $PORT” are there because Cloud9 requires these so it can dynamically assign an IP binding address and port number. At this point, Rails is running.

Next, the tutorial discusses the architectural model that Rails runs off of: model-view-controller. I am going to try to put the idea into my own words. A user is browsing the web and comes across a site running on Ruby on Rails. They make a request to the site. This request goes to the controller. The controller has to make a decision, depending on what the request is. If something simple is needed, a view is immediately sent back. However, if something dynamic is required, the controller interacts with the model, which communicates with a database to get the information needed. This is then passes on to the controller that renders it to a view and sends it on to the end-user. This may be off. I’m still learning!

Now we are going to make our first app. Basically, the app replaces the generic Rails page with one that says “hello, world!” This will be done by adding a controller action that renders the string “Hello, world!” on the page. This is done by first going to the application controller in the directory and adding the following code:

So, “def hello” defines the action we are about to ask for as “hello.” Then, “render text: “hello, world!” is given as the action to call when “hello” is asked for. Next, we have to go to routes.rb and add some code to tell the Rails router where to send the requests. This is doneby writing the following:

This tells Rails to send the root route to the hello action in the Application controller. We defined that action above. As expected, when open the page in a browser, “Hello, world!” is displayed.

Next, the tutorial goes into placing the application under version control. This allows us to track changes to the project’s code, to collaborate more easily with others, and roll back if mistakes are made. To do this, we use Git.

First, we need to set ourselves up with Git. This is done with the following:

Of course, I used my email. I get enough spam….

The first two lines are necessary to register yourself if publishing repositories publicly. The third line, “git config –global push.default matching” ensures forward compatibility with an upcoming Git release. The fourth line, “git config –global alias.co checkout” creates the ability to use “co” instead of the “checkout” command.

Now we are ready. First, we initialize the repository using the command:

Next, we add in all the project files using the command:

Next, we need to tell Git to keep the changes we have made. This is done by telling it:

The flag at the end, “-m” allows you to add a message for the commit.

After this, I went to Bitbucket and made an account, then created a private repository. I had to add in my ssh key from Cloud9 and then added it to Bitbucket. This made adding the repository I created so much easier. I used this command:

BitBucket actually tells you what commands to use to add BitBucket as the origin for the repository, and then to push everything up. They are essentially as follows:

Git also allows branching so that you can experiment without doing damage to the original app. This is done by using the following command to checkout the branch:

This tells git to checkout the branch, indicated by the “-b” flag and then names the branch “modify README.” Then we made some changes to the README file in the application to make it appear on BitBucket. First, we changed the name of the README file using the Unix command “mv.”

This command renamed the README.rdoc file to README.md.

Next, we edited the files using HTML markup. Then, we ran the command:

that showed we had made changes. Then we used this command:

to commit all of the changes made, indicated by the “-a” flag, and added a message using “-m” flag saying what changes were made.

Then we need to merge the branch into the master. This is done by first checkout the master with the:

command, and then merging them via the

This merges the branch titled “modify-README” with the master. Now that it is merged, we can delete the branch using the command:

where the “-d” flag stands for delete. Now, we can push the changes to BitBucket. This can be done this time with a simple:

Finally, we are ready to deploy the application to Heroku. After the setup of Heroku on the system it was very simple to deploy it. First, we create a space on Heroku for the app to live using the command:

and then we deploy using this command:

And that is it for Chapter One. This review took a long time, but I really think I understand better what I did the first time through.

Day 8: Finishing Chapter 3 of the Rails Tutorial

I really feel as though I am getting the hang of this.

Today I completed the Ruby on Rails Tutorial Chapter “Mostly static pages” I also breezed through the exercises and I can feel my confidence rising. I really like the way Michael Hartl does the tutorial, its like it just builds naturally.

This chapter had to do with the creation of static pages in Ruby, plus adding some dynamic elements to it. It also included the first bit of embedded Ruby gem:

that the tutorial used to create individualized title pages for the three static pages we built. It did this through another line of code:

I also really liked how the tutorial used tests to figure out what needed to be added. This was done through a command:

As well as a Guard file. When I read through the way we would be testing this stuff I was amazed. Look at this:

This is using logic to test whether something is happening. If it is, success. If not, fail. It is so simple yet so powerful. I am really enjoying this!

Day 7: One Week of Learning Web Development

I have been at this one week and I am really enjoying myself! I am learning a lot and believe I am slowly getting a handle on what I am doing following along with the Ruby on Rails tutorial. I am in the middle of Chapter 3 right now, working on adding static pages in Ruby on Rails. I also did the exercises from Chapter One and Two today to get a refresher prior to starting this chapter.

I also had some issues that came up with using Cloud9 and learned some more by fixing them. Those are always fun!

Day 6 Update

I continued with the Ruby on Rails Tutorial by Michael Hartl today. I completed Chapter 2, which is creating a Toy App. I am so far very impressed with Ruby on Rails. In this tutorial, I created an extremely simple user registration page and a micropost page. I can already see how powerful Ruby on Rails is. I also see that there is a beautiful simplicity to the way it is designed. I feel like the commands are intuitive, which I really enjoy.

Setting SMART Goals and Objectives to Help Learn Web Development

Those who have been following this blog, and yes I know this blog is new so all 2 or 3 of you, know that I am coming from a social work background. My current position is working with teen boys in a foster care group home. Before this, I worked with Child Protective Services. I also worked for a few years as a case manager on a Crisis Stabilization Unit. All of these had some similarities, but for our purposes, the similarity that matters is the use of SMART goals and objectives. SMART goals and objectives are great for setting goals that you can ACHEIVE!

Let’s look at a goal most of us newbies to Web Development have:

“I want to get a job as a Web Developer.”

Great! Good for you, but this goal is missing many elements that can help you measure your progress or make it more achievable. First, let’s look at what a SMART goal is. SMART stands for the following:

  • Specific
  • Measurable
  • Assignable
  • Realistic
  • Timely

Let’s briefly look at each of these.

Specific means you need to avoid a broad topic and drill down to a specific area you need to improve. Look at the goal above. Web Development can mean SO much. Front end? Back end? Ruby? Python? Full stack? You need to focus! what counts as a job? Freelance? Contractor?

Measurable means you need to look at the goal and see what progress you have made. Again, the above is so open to interpretation. What is your timeline? Have you failed the goal if you have not reached it in one year? Ten years? 6 months? How do we know when the goal will be met?

Assignable means that the goal says who will do it. The goal above has that, and honestly this one is straightforward. You will be writing a personal goal. “I” works!

Realistic looks at whether the goal can be achieved. Many of these elements are dependent on others. The above goal may be realistic, but right now, it is too broad to tell. If you make the goal to be hired as a junior Web developer, then it may be realistic. If you set your goal to be that you want to go straight from self-study to being hired as a senior developer, maybe not so much.

Finally, timely means we need to specify a timetable for this goal. The above, as I mentioned, lacks this element. Measurable from above can mean a time table, but it also means that you need to know how much progress is being made toward the goal being timely.

Let’s look at what a SMART goal for this would look like:

“I will get a job as a junior Web developer, by studying Ruby on Rails at least 2 hours a night by February, 2016.”

In this, I have made the goal specific, I have placed a timetable, I have made it measurable, and hopefully realistic. Underneath this goal, I need SMART objectives that build toward it, such as:

“I will complete the Ruby on Rails tutorial by Michael Hartl by June 30, 2015 by studying it 2 times a night.”

and

“I will blog daily about my progress on studying Ruby on Rails, including details on what I studied that day until June 30, 2015.”

June 30, 2015 is when I will reassess the progress I am making. It is OK to readjust expectations. When I first set these goals and objectives, I may overreach or underreach. What if on June 15, 2015, I have completed the tutorial? I sit down and make new goals and objectives! If I need more time, on June 30 I will set goals to finish it, hopefully with better perspective.

I hope this helps. I am by no means an expert, and I am sure someone out there has done a better job setting goals or writing them. If so, let me know. I am always open to learn more!