Installing NMatrix from SciRuby

I am getting involved in the SciRuby project. One of the main sub-projects is called NMatrix. It is a library for linear algebra.

NMatrix uses some C/C++ and Fortran libraries. The installation instructions for Linux refer people to the installation pages for SciPy. To a certain degree, SciPy is a competitor to SciRuby. I think it is a bit ironic that they refer people to SciPy for instructions.

SciRuby requires two libraries called ATLAS and LAPACK. The SciPy instructions go over how to compile and install the libraries from source. I really do not like to do that. I prefer to use Ubuntu packages for C/C++ libraries as much as possible. I got it to work, and here are the steps I went through.

At the time of this writing (2012-07-31_22.27.46), I am using the precise version of Ubuntu.

I installed a couple of packages first: libatlas-dev and libatlas3gf-base. I ran

and got a couple of errors:

I did some googling, and I found out this is because it cannot find libraries called cblas and atlas. “ld” is the GNU linker. I had a few *so.* files in /usr. According to what I found out, ld wants the files to end with .so. For some reason, there are a lot of library files in Ubuntu that end with .so.1, or so.0. There are a few that end with .so.3gf.0. The “3gf” means they were made from Fortran source.

I found the library files by running the “locate” command:

So the commands that I ran were

Then I ran

and eventually I got this:

There aren’t any more space shuttle flights. That seems like an odd warning.

After that, I installed the pry gem, and ran this command:

I was able to run the commands on the NMatrix wiki on Github.

 

A Bit More Ruby Testing Archaeology

In two previous posts, I wrote about my frustrations with methods in Rails tests that just seem to float in space and appear out of nowhere, and that the Hartl tutorial mixes RSpec and Capybara, and that it would be nice to know which is which.

In the last post, I wrote that sometimes when you try to find which class contains a method you get “Object”, which is not very helpful.

Here is the output:
The “sign_in” method is from one of the tests in the Hartl tutorial. I was explaining this to someone about an hour ago, and I realized why self.method says that  “sign_in” is part of the “Object” class: Because it is not defined in a class. In the application, Hartl defines it in a module, but in the tests it is simply defined in a file that contains neither  a class or a module.

In the root of the Rails tutorial app, you can run a grep command to find that method:

This will give you the following result

So here is the file spec/support/utilities.rb:

As I stated, in app/helpers/sessions_helper.rb the method is in a module called  SessionsHelper

More Ruby Testing Archaeology

I made some progress on this.

If something is a class, like “page” in the Hartl tutorial (like on say spec/requests/static_pages_spec.rb)
you could just do

which would give you

I don’t know why I didn’t think of that before.

In that file he also calls some voodoo methods. In before do he has:

and

sign_in and visit are methods. You can get these by doing this:

or you could do that on one line each like this:

Which will give you:

Granted, getting “Object” is not too helpful. But it is some progress.

Someone on the Rails core team has a post about finding methods here.

Ruby Testing Archaeology

One of my frustrations with Rails is that there are few if any ‘include’ or ‘require’ statements. Now I am going to do something about it. At least something for myself.

I went through the Hartl tutorial (as do most Code Academy students, and many aspiring Rails devs). He uses RSpec and Capybara for testing. RSpec can be used for any Ruby project, while Capybara is used for web projects. I think it can be used for other Ruby web frameworks, and other testing frameworks besides RSpec.

One thing I find in the tutorial and the tests is that Hartl will just call methods and instantiate variables. I don’t always know whether he is calling something from RSpec or Capybara.

At one point, he says this: Because of subject { page }, the call to should automatically uses the page variable supplied by Capybara (Section 3.2.1). This is in refernce to the  file spec/requests/static_pages_spec.rb.

But where in Capybara does “page” come from?

You see it in code like this:

The “subject” method is used for the models as well:

But there is no “page” model, or a “page” factory.  But it raises another question in my mind: What is “subject”?

I am pretty sure “subject” is from RSpec, either
http://rubydoc.info/gems/rspec-core/RSpec/Core/Subject/ExampleGroupMethods#subject-instance_method
or
http://rubydoc.info/gems/rspec-core/RSpec/Core/Subject/ExampleMethods#subject-instance_method

In the Hartl specs (2nd edition) you see this (via grep -r subject * | grep page)
requests/static_pages_spec.rb:5:  subject { page }
requests/authentication_pages_spec.rb:5:  subject { page }
requests/micropost_pages_spec.rb:5:  subject { page }
requests/user_pages_spec.rb:5:  subject { page }

In the Hartl tutorial, at say, like http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:pretty_rspec, he says
Because of subject { page }, the call to should automatically uses the page variable supplied by Capybara (Section 3.2.1).

So where does “page” come from? I am looking around the Capybara docs, and on http://rubydoc.info/github/jnicklas/capybara/file/README.md I see this:
Full reference: Capybara::Node::Matchers

Capybara has a rich set of options for querying the page for the existence of certain elements, and working with and manipulating those elements.

page.has_selector?(‘table tr’)
page.has_selector?(:xpath, ‘//table/tr’)
page.has_no_selector?(:content)

page.has_xpath?(‘//table/tr’)
page.has_css?(‘table tr.foo’)
page.has_content?(‘foo’)

After some googling and some digging, and it looks like “page” is part of the Capybara::DSL module:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/DSL

It looks like it returns a Capybara::Session object.

At some point I will make a separate page or post with just the methods that pop out of nowhere, and whether they are RSpec or Capybara.

 

More On PostgreSQL and Rails

I had already posted on getting a Rails app to work with PostgreSQL. For that app, I was starting over from SQLite.

But now I am starting to use PostgreSQL from the beginning since Heroku runs on PostgreSQL. I learned more about this from RailsCasts.

First you have to use the –database option when you make the app:

Then you need to edit the config/database.yml file. I have not sent my app to Heroku yet, so I comment out the prod stanza. For the test and development stanzas I change the username to the username that I am logged in as on my Ubuntu laptop.

Then I run

Then you can run

Don’t forget to run this after each migration as well:

 

 

Ruby And Android

I have been thinking about getting more into Android. I have looked at the tutorials, and it looks like a laborious process to get anything done.

I was speaking with a local mobile developer who develops for just about every type of phone (Android, iPhone, Windows and BlackBerry). The guy owns 40 phones. He said that you should just do things in the standard way laid out by Google.

Sometimes I wonder if that is the best idea.

Android development seems like a step backwards. You can think of Java as C++ without the hard stuff. Android is Java with an emphasis on the hard stuff. There is a lot of XML, and a lot of anonymous inner classes. A lot of the reasons that people have been leaving Java are all over Android.

I am looking at RhoMobile and Ruboto to do Android development with Ruby.

 

Well Grounded Rubyist for 2012-07-03

Here are a few more somewhat random notes from chapters 7 and 8 of Well Grounded Rubyist:

Look up the “spaceship operator”.
== tests for equality of contents of strings. equal? tests to see if the strings are the same object (this is how “equal” usually behaves)
String::crypt uses DES. Is DES any good?

begin/rescue/ensure/end is like try/catch/finally in Java. The “ensure” block will always be called.
Bang methods change the caller, like reversing a string instead of returning a reversed string. I thought they swallowed exceptions (I think that is what they did for me in tic-tac-toe).

String interpolation does not happen with single-quoted strings. Other ways to make strings:
%q{}
%Q{}
%{}

Another local developer said that she found Well Grounded Rubyist to be easier to follow and better than the “PickAxe Book”. So far I agree. I have gotten farther in WGR than the PB.

Image from World Digital Library, assumed allowed under Fair Use. Image from the Ashburnham Pentateuch, or Tours Pentateuch, a Latin manuscript of the first five books of the Old Testament from the 6th century or 7th century. Its place of origin is unknown.

Ruby Announcements

They are not announcements about Ruby in general, just about me.

First off, I submitted two proposals to speak at Lone Star Ruby Conf in Austin, Texas in August. One was accepted, and one was rejected. I started work on my presentation. I may also start the other presentation later at some point.

Also: The RSpec Study Group that I keep talking about is finally getting off the ground. The first day will be July 9th. Expect more details soon. It may be known at the Chicago Ruby Testing Group, since we will talk about more than just RSpec. We will still focus on Ruby testing technologies, so Python, PHP, dot NET and Java people will be out of luck. I am working on this with another Code Academy alum, with some help from a couple of Code Academy mentors.

 

Coding Without Testing – just for now

Since I got done with Code Academy, I have not done as much coding as I should be doing.

I am still working on getting the Ruby Testing Group off the ground. I am going through The RSpec Book, and I am going through The Well Grounded Rubyist. I am still going through all my stuff (I am amazed at how much I have accumulated).

I am still learning new stuff. I think now that I have perhaps subconsciously inhibited myself from coding because I am not ready to test all parts of a Rails app from the beginning. I still want to be a great coder, and I know that requires writing tests, but I think that I need to start building. Even if that means not writing tests right now.

More Well Grounded Rubyist

When you call one method from another in the same object, you do not need to write

You can just write

The exception is if the method name ends with an equal sign. Calling

calls the instance’s method. Using the form

 

defines a class method. To call those, you must use

to call it from another method.

Global variables start with a dollar sign like this:

Class variables start with two at signs:

The class variable is actually for the class hierarchy, not just the class. So if you have a variable

in the Shape class and Circle inherits from it, then they will share that variable. For a subclass to have its own class variable, reference it with the word “self”:

The book also talked about public, private and protected methods. You can just list the methods you want to be private with the “private” keyword:

And you can call them as regular methods in the object.

The book also had a section on protected methods, but honestly I did not read it too carefully. Protected methods started in C++. I have read that the designer of C++, Bjarne Stroustrup, regards them as a mistake, and that if he could start C++ over again he would not put in protected methods. Good enough for me.