Ruby Tip: Instances

So I was working on a Rails project, helping someone upgrade from Rails 2 on Ruby 1.8.7 to Rails 3 on Ruby 1.9. There have been a few changes in the APIs. At one point, I was getting some exceptions. I needed to see what variables were available to an object. There were ifs and mixins and supers, so it was not always clear what I could call in a “puts” statement and what I could not.

So here is how you print out a list of the instance variables, and get a value:


2013-02-04 Austin Ruby

Tonight I went to Austin Ruby. At the end there was a pairing exercise.

The kata can be found on the Austin Ruby github page. It involved threads. I thought the point of using a high-level language like Ruby was to never deal with threads.



RailsPress 2013-01-30 Update

I attended OpenHack Austin tonight, working on RailsPress.

I decided to cheat by logging SQL statements.

You can turn the logging on and off in the db and check it like this:

It will write the logs to /var/log/mysql/mysql.log

It is a bit cumbersome. So far I am trying one thing at a time. I will turn on the logging, do my action, and turn the log off. I may give up doing it this way.

I was grepping for SQL statements in the code. I used “cat” to put the results into a file. So I suppose I could go back and forth.

Update On RailsPress

I started a project to migrate WordPress to Rails. I called it RailsPress.

I installed WordPress on my laptop and made a couple of posts.

I found a gem called rmre that can reverse engineer a database and produce ActiveRecord classes and a schema.rb file. I made a separate gemset with RVM, but it still took some doing. I ran it a couple of times since it kept complaining about a dependency. Finally it complained that it could not find the 2.9 .0 version of the mysql gem. I added that to the Gemfile, ran bundle install, and it complained it could not find the 2.8.1 version of that gem. So I added that version manually via the gem command. I got it to work, so I added the model classes

Then I started another project called rails_press_work. I used the schema.rb file I got from rmre to get the fields for the commands to generate the scaffolds, and used them to generate models, controllers, views and rspec files. I copied those over to the main project, and got the tests to work. I did that for two models so far. I had to edit the valid_attributes methods in the controller specs.

I then noticed that when you revise a post that WordPress makes two entries in the wp_post table. So I will have to do some digging. I got the Apache log4php project, and I got it to work on my local WordPress install. I will see if I can figure out how to keep the post revisions straight.


Porting WordPress to Rails

I am thinking about porting WordPress to Rails.

I was at Lone Star Ruby Conf in 2011. I was talking with another guy about CMSes and Rails. He said that it might not be too hard to write classes to interact with the WordPress schema. (Having looked at it for a couple of hours, I am thinking it might be pretty hard.) I think we got on this topic because when Rails came out, a lot of people thought that Rails would displace Java. I think that Rails has really displaced PHP more than anything else. PHP still rules the roost in CMS. He said that WordPress is a killer app for PHP.

Recently I started thinking about it again. I am on a few lists for local Ruby/Rails user groups. People ask about Ruby/Rails CMSes a lot. None of them ever seem to gain any traction. WordPress is still king. Perhaps a port of WordPress will gain a lot of users.

Granted, I would only be able to port what WordPress comes with out of the box. All the themes and plugins would have to be provided by others. Perhaps the Rails community as a whole has no interest in CMSes. Maybe I am out of my mind for considering it. But I may give it a couple of days to see what happens.

I found a gem that will reverse engineer ActiveRecord classes and a schema.rb from an existing database. So at least that part should be covered.

Update On RailsApps Tutorials

I started using the RailsApps tutorials.

They are pretty good, but I noticed a glaring flaw: There is only one model, the User model. You don’t just create an app where users do nothing.

So I started working with a couple of them to add another model. I also wanted to be able to test them. I wanted to make Capybara tests to test actions for both authenticated and unauthenticated users, similar to what Hartl does, but with a standard authentication gem. Some of the RailsApps use Cucumber, but I have noticed that most Rails developers are not interested in Cucumber, and it looks like Capybara can do some of the things that people do with Cucumber.

One of the apps uses Devise. I have a Github repo based off of the rails3-devise-rspec-cucumber RailsApps tutorial:
The “add_book” branch has the authentication tests. The model is called Book.

Note 2015-06-14_12.46.25: Now at – HerokuPinger now at

I have another app that uses Omniauth called Heroku Pinger. The “second_attempt” branch has some tests that I think do what I am trying to do:

People will put up a free app on Heroku, but sometimes when they give out the URL the app will not be very responsive since Heroku puts free applications into a hibernate mode. This app was intended to allow people to have a few apps get pinged every hour. John McCaffrey told me that is not very sportsmanlike since the point of free apps is to not suck up too many resources. He is correct, but I decided to keep going because the real point was to work with OmniAuth, and eventually work with mocking and stubbing tests as well. Besides, you could ping any site, not just a Heroku site.

For the Devise app, it turned out to be pretty simple. I did some googling on how to get a Devise session in Capybara. A lot of the results said you had to mess with a Devise dependency called Warden. They say you need to put the following line in your tests:

I think that some of that information might be out of date, because I went back and commented out those lines, and the Capybara tests still do what I intend them to do. Perhaps updates to the gems made that unnecessary.

The magic is to get an instance of Capybara::Session and pass it around as you need it. I put the code in spec/support/session_provider.rb:

The Capybara::Session is stored in the “page” variable. You can use it like this:


I have a slightly more involved test that actually adds a book and checks that the count of Books has increased by 1.

The OmniAuth test took me longer. Part of is I later realized was that I was using methods like “get” from the Controller tests, and not the Capybara “visit” method. Capybara has a “get” method, but for some reason I got nowhere with that. I wasted a lot of time thinking it would work.

The SessionProvider class is in spec/support/session_provider.rb and looks like this:

In the tests that use this, you just do this:



I looked at the OmniAuth wiki and a few posts on StackOverflow and tried and re-tried several¬† times, until I had my “visit” epiphany.

Review of Practical Object-Oriented Design in Ruby

I did a review of the book Practical Object-Oriented Design in Ruby: An Agile Primer  by Sandi Metz. Here it is below.

In her afterword, she writes: “…if this book has achieved its goal, you think differently about objects now than when you first began.” This book did make me think about object differently. For the most part, very little in this book was new to me, but it did present things in a new way.

Maybe it is due to years of ORM frameworks, but I had thought of objects primarily in terms of their data fields, and the methods are there to control how they are manipulated. Sort of like a C struct and functions in one file.

She repeatedly states that we should think of objects in terms of the messages that they can receive. The data seems to be secondary.

The book mentions design patterns, but does not spend much time on them. She mentions the SOLID principles explicitly a few times, and some of the book’s chapters cover a SOLID principle.

There is also a chapter on testing. I have used RSpec, and the book uses MiniTest. I thought that because of that I would not get much out of that chapter, but I was wrong. There is a lot of good information in this chapter as well. Another interesting perspective that I got from the testing chapter is the the point of testing is not to reduce bugs or to document, but to reduce cost.

She gives a lot of rules and principles throughout the text, and also says that they are general guidelines. If you are in a situation that requires

This book might be a bit much for someone new to Ruby or OO programming, but if you have been doing Ruby and/or OO for a while, I think this book is worth it. You might not learn something new, but you will probably see things differently, which can be just as useful.


Another Update

Here is an update on what I have been doing lately.

I am pretty done with Java, but I looked at the site for a Java library that claims to be thread-safe. I looked at the code, and they do their own thread pooling. I might keep a list of Java apps and libraries that do threading. But I still think that Akka might be the way to go.

I am also going through a Ruby book to review it. I have to post something in a few weeks.

I might also present in a few months at Austin On Rails. I kind of sort of agreed to present on JRuby and Torquebox.


Finally Done with The Well-Grounded Rubyist

I am finally done with The Well-Grounded Rubyist.

Part of the reason is that I moved to Austin. That took a lot of my time. Plus I got distracted with other project. Plus the last few chapters were about metaprogramming and callbacks and giving methods to instances that are not defined in the class and all the voodoo that really does not appeal to me. But I might see code that uses that stuff, so it is good to at least see it. But it is a real slog since that stuff really does not interest me that much.