Update On RSpec

I do not think there will be a meeting of the RSpec group this weekend. I plan on meeting my non-technology friends.

I was going through the Hartl tutorial while in Code Academy. I was planning using some of his tests in our app, but I could not get it to work. So I realized I should probably go through the RSpec book and learn me some more RSpec. As well as some Cucumber and Webrat or Capybara.

I plan on going through the RSpec book myself before really getting rolling with the RSpec group. I will put the code up to Github. I do not know if I will get all the way through it, or if I will skip around. The book talks about Webrat to simulate the browser. It seems like the world has moved on to Capybara. But I will make a valiant attempt.

Image from Wikimedia, assumed allowed under Fair Use. Image from the St Augustine Gospels, a 6th century manuscript made in Italy in the 6th century, brought to Britain by the Other Saint Ausgustine.


Update on RSpec TDD Study Group

We had the first (informal) meeting of the RSpec study group. Three other guys showed up. All three plan on being mentors at Code Academy.

The plan is to mix lectures and “labs”. One of them had introduced RSpec/TDD to people while pairing with them, and felt it was a good experience. I thought we should mix in some lectures since there is probably more to RSpec than the

stuff we saw in the famous Hartl tutorial. So we will go over a few chapters in The RSpec Book first before setting up a formal schedule. We will probably cover some Cucumber and Capybara as well.

I mentioned during the Code Academy Demo Day that I was planning on starting an RSpec study group. One of the people in the audience was Bobby Norton, who spoke at CJUG about a week before. He told me I should talk to David Chelimsky. He is here in Chicago, is a contributor to RSpec and one of the co-authors of the RSpec book. Looking at his Github profile, I now realize that I had seen him around at events quite a few times. In all of the Chicago Ruby meetups and other events I had gone to, nobody ever told me who he was or that a giant walked amongst us.

I hope he doesn’t think I’m a jerk.

Last thought: Agile shops say you should program “Tests -first.” Should languages be taught  “Testing frameworks-first”?

Image from Wikimedia, assumed allowed under Fair Use. Image from the St Augustine Gospels, a 6th century manuscript made in Italy in the 6th century, brought to Britain by the Other Saint Ausgustine.


What Happens After Code Academy?

What happens after Code Academy?

At this point, I have no idea.

The goal is to get a job, perhaps an apprenticeship that leads to a job.

Right now I am trying to nail down my living situation. I have to find some storage for my stuff. Granted, if I am putting stuff in storage that means I probably need to get rid of some stuff.

I may go to Rails Conf.  First I have to get depressed about the hole it will put in my savings.

I am also trying to get an RSpec study group started here in Chicago. I could start building apps right and left, but I would like to do it the right way, with TDD and BDD.

As far as jobs, I would like to get a Ruby/Rails job. I just spent a lot of time learning it. I am not too thrilled with Java these days. There are too many web frameworks that are awkward to test and develop with. There are a lot of companies using old versions of these frameworks as well. There are too many ways to do concurrency. I think over time the Akka library will push a lot of stuff aside.

Sidebar: Why didn’t Sun just use Actors for concurrency in Java from the beginning? Or add them later instead of adding another concurrency API for every other version of the JDK? It is interesting that this is so convoluted in Java and there are so many ways to do it, yet when you talk to the Erlang people, they say, “We just use Actors.” And they have been happy with that for more than twenty years.

I was talking several months ago to someone at Groupon about threading and concurrency in Rails and Ruby. He said that those are things that they do not really worry too much about. He said, “Come to the dark side. We have cake.” That started my journey to Ruby and Rails.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Garima Gospels, an Ethiopian Gospel manuscript created around 500 AD. It is believed to be the oldest complete Gospel manuscript.

Connecting Rails and Postgres

I am working on a project for Code Academy with a few other students. We decided to use PostgreSQL, since that is what Heroku uses. One of the other students posted some directions on his blog. I could not follow them since I run Linux.

I had already run apt-get to install some libraries, so I will skip that.

I put this into my Gemfile:

I put this in config/database.yml

But when I ran

I got this error:

I had to do a lot of googling. I won’t go over some of the other errors I got, so I will try to list some commands that will help me to get Rails working with Postgres on Linux boxes in the future.

You must create a user that has the same name as the username that will be running the rails app. It must own the database, and have permissions to create and alter databases. For some reason I did not need to enter a password in any Rails file. Even so, things seemed to work.

In order to use Postgres, I have to type this:

I would like to log into postgres without sudo-ing. I can log into MySQL as root from my main user ID.

To create the user:

To see users, run the metacommand \dg

Then create the database:

To list the databases, run the metacommand \l

After this, you should be able to run the commands to create, populate and reset the databases:

Image from Wikimedia, assumed allowed under Fair Use. Image from the Cotton Genesis, a Greek manuscript of the Book of Genesis created in the 4th or 5th Century. It is called the “Cotton Genesis” because at one time it was owned by a man with the surname “Cotton”; it is not made of cotton.

Rails and MySQL

I decided to use MySQL with Rails on my Ubuntu and Windows machines. On Windows I use RailsInstaller, and I was able to install the gem with

It even seemed to compile stuff, or it got a precompiled gem. I saw this in the console:

For Ubuntu, there were a few more issues. First I has to install some packages:

Then I could run the gem install command:

Then in the development section of config/database.yml I had to comment out the following line:

I added mysql2 to the Gemfile

Then I could generate scaffolds:

Here are the create statements and some helpful commands:

Rails Routes

Here are the Rails routes:

/actors GET index actors_url
/actors POST create actors_url
/actors/1 GET show actor_url(@actor)
/actors/1 PUT update actor_url(@actor)
/actors/1 DELETE destroy actor_url(@actor)
/actors/new GET new new_actor_url
/actors/1/edit GET edit edit_actor_url(@actor)

Pickaxe Book Chapter 05

This chapter is about inheritance, modules and mixins.

To make a subclass:

You can invoke parent class methods with “super”, just like in Java. You will usually want to call “super” in the initialize method if you are inheriting.

On page 95, it would be nice if the “private” keyword was tabbed in a few spaces.

Modules provide namespaces and allow some reusability. But unlike packages in Java it seems like they are similar to Ruby classes with two differences:
1. They have the word “module” in front of the name instead of the word “class”
2. Methods defined in the module have the module name in front of them. So if a Trigonometry class had a method “sin(x)” tyou would see this:

A module would do it like this:

If you use a module constant, you need to specify the module name and use the double-colon operator:

Modules cannot have instances, so I guess they are like static classes in Java.

Is there inheritance for modules? I may find out

A mixin is when you include a module in a class. Then you can call the module’s method from a class instance as if they were parent methods.

The book talks quite a bit about the Enumerable mixin. I guess all you have to do is provide an “each” method, and you get all the iteration methods in collections (Array and Hash).

At the end of the chapter, they recommend designing programs with composition (has-a) and delegation (uses-a) instead of inheritance (is-a). They seem to say that mixins are more of a composition technique than an inheritance technique.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Ambrosian Iliad, a 5th century manuscript of the Iliad.

Pickaxe Book Chapter 04

This chapter starts out with arrays (square brackets) and hashes (curly braces), which are important to know. It says that blocks when combined with collections are good iterators. The elements in arrays and hashes can be any type. Using different methods, arrays can be different data structures: sets, queues, dequeues, FIFO queues, instead of using different classes. In Ruby hashes, it keeps the objects in the order they were entered. Keep the names of the block parameters unique. You could use a colon, but I think it is better to keep them unique.

A block can be empty:

But you must put in a block if a method has “yield” in it, even if yield has a parameter

To keep track of how many times you have gone through the loop, instead of each, use each_with_index. You will need a second variable for the block: index to track the count.

I did not quite get the “inject” method, and I skipped over some of the stuff about enumerators since it said I could.
Class methods start with the word “self” in front of them

At the end it gets into using blocks for closures and lambdas. Do we get into that with Rails? I think I get this stuff, but sometimes I have to look at it for a few minutes to get it. I don’t think I really know too well. Plus it appears there is another syntax for Ruby 1.9, which makes it more confusing.

There is an encouraging note at the end that if you don’t get it now, it’s okay. Thanks, Dave!

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.

Pickaxe Book Chapter 03

This chapter was pretty easy. Mostly about objects and variables.

So what is an attribute? It seems like it is another name for an instance variable. They start with an “at” symbol. I am still not too clear what a symbol is.

I did not pay too much attention to the part on protected methods. I read somewhere that Bjarne Stroustrup, the creator of C++, at one point said the “protected” keyword was a mistake. Ever since then I have not paid much attention to protected.

Pickaxe Book Chapter 02

Here are some notes on Chapter 2 of the Pickaxe book.


Single quoted strings take less processing, no interpolation
Return value of a method is the last statement, so you may not always want/need to put a “return” statement in there.
Local variables, method parameters and method names all start with lower case or underscore.
Global variables start with a dollar sign.
Instance variables start with an at symbol.
Class variables start with two at symbols.
Class names, module names and constants start with uppercase letter.
Character after @ may not be a digit
Multi-word instance variables get underscores between words, multiword class names get camel case.
Method names may end with ?, ! and =, although these have special meaning.

Arrays: like lists or arrays in Java, unlike primitive arrays you do not need to specify the size ahead of time.
Hashes: Like maps in Java.
The values do not need to be of the same type.
Shortcut for arrays:
a = [‘ant’, ‘bee’, ‘cat’, ‘dog’, ‘elk’]
same as
a = %w{ant bee cat dog elk}

Symbols: A lot of people in class had trouble with this concept. I am not too clear on it myself. They can be used as keys for hashes.
There is also a new Ruby 1.9 syntax for hashes.
Here is the old way:

Now you can do this as well:

This has caused some controversy on some Ruby mailing lists.
There are no braces for control structures. Instead Ruby uses the keyword “end”.
Next the book covers regular expressions. I am not going to get into details here. I have seen this before, although I know there is a lot to see.
Blocks are a bit weird to me. I will have to look them up somewhere. It seems to me like it is just another arg to a method. Is it special because it is code?
I altered some of the code from the book to make a method take a parameter as well as a block:

The book says that blocks are used to implement iterators. I can never remember how to iterate through stuff. I will have to make a separate post about that.

The I/O section talks about printf. I thought the whole point of Ruby was not to deal with C?
puts prints to the terminal, gets gets from the terminal.
The command like arguments are in an array called ARGV.

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.