Book Review: Exploring Everyday Things with R and Ruby by Sau Sheong Chang (O’Reilly)

Exploring Everyday Things with R and Ruby, as the title suggests, is a book about data exploration, written in a very easy and very unusual way, that will make it hard to put down.

In the beginining it starts with a short introduction to the two languages used to achieve its purpose.

It gives a short explanation about the reason of using Ruby, followed by installation instructions and some basic ruby information. After that there is a short introduction to Ruby’s UI toolkit called Shoes.

The second initial part covers the other language to be used in the examples to follow, R. There are again the reason for picking R, installation instructions and a brief introduction to the capabilities of the language and especially the data analysis and graphing abilities.

Following the first two parts, is where the book starts to get really interesting and fun and it will certainly make you want to try the examples worked on.
Subjects like ‘Offices and Restrooms’ which is about deternining the correct people-to-restrooms ratio, ‘How to Be an Armchair Economist’ about a market economy simulation, ‘Discover Yourself Through Email’ dealing with email data mining, ‘In A Heartbeat’ for measuring the hearbeat, including a homemade digital stethoscope, ‘Schooling Fish and Flocking Birds’ a simulation of the Boids algorithm in Ruby, and finally ‘Money, Sex and Evolution’ an entire artificial world populate by the roids of the previous example.

All of the examples are fascinating, something that I would never imagine it would be possible to simulate before reading this book, and also include information about their specific fields.

So in conclusion, it is a very enjoyable, interesting and out of the ordinary book, helped greatly by the author’s unique writing style, and one that I would recommend to anyone with an interest in Ruby or R to read.

build association for has_one in rails

Problem

You would like to use the build method for creating an association (details) that belongs to another assocation (user).
When the user has many details then you would have something like the following:

class User < ActiveRecord::Base
  has_many :details
end

then you could do:

User.details.build

but when you have:

class User < ActiveRecord::Base
  has_one :detail
end

would throw an error:

NoMethodError: undefined method `build' ...

Solution

In order to be able to use the build in a has_one relation you would need to use like:

User.build_detail

libv8 error in new rails installation

Problem

You are getting a libv8 installation error in a new rails 4.0 application in an older linux installation

An error occurred while installing libv8 (3.16.14.3), and Bundler cannot continue.
Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling.

Solution

This is causes by the latest gem version of the therubyracer dependency on the libv8.
You can get over it by specifying version 0.11.4 for the gem as in:

gem 'therubyracer', '~> 0.11.4', platforms: :ruby

and then running bundle install again.

WARNING: Nokogiri was built against LibXML version 2.9.0, but has dynamically loaded 2.9.1

Problem

You get the following warning after an update to your system (ie Ubuntu 13.10):

WARNING: Nokogiri was built against LibXML version 2.9.0, but has dynamically loaded 2.9.1

Solution

Uninstall your nokogiri versions:

gem uninstall nokogiri

and then run install it again (inside your application if you use bundler) with:

bundle install

db:migrate or db:schema:load for Rails project with many migations

Problem

You have a rails application with many migrations build over time and you want to recreate the database from start. Should you be using the normal way of running the migrations (db:migrate) or the one that loads the actual schema to the database (db:schema:load).

Solution

According to the book Rails 4 in Action (MEAP v9 page 146) :

The bin/rake db:migrate task runs the migrations and then dumps the structure of the database to a file called db/schema.rb. This structure allows you to restore your database using the bin/rake db:schema:load task if you wish, which is better than running all the migrations on a large project again! NOTE

NOTE: Large projects can have hundreds of migrations, which may not run due to changes in the system over time. It’s best to just use the bin/rake db:schema:load.

rails `require’: cannot load such file — mysql2/mysql2 (LoadError)

Problem

You want to use a different ruby version from the one you have initially installed and build your application with, but when you change it in your rvm installation you get the following error:

..gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require': cannot load such 
file -- mysql2/mysql2 (LoadError)

Solution

To make it work again you will have to uninstall the mysql2 gem and install it again in the new ruby version with the option –platform=ruby.

rvm use ruby-2.0.0-p247
cd my_project
rails s
..... gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require': cannot load 
such file -- mysql2/mysql2 (LoadError)
gem uninstall mysql2
gem install mysql2 --platform=ruby
rails s
=> Booting WEBrick
....

RVM and RubyGems versions

Problem

You would like to upgrade your rubygems version from 1.8.17 to the latest version 2.0.7 for example.

Solution

You can upgrade to a specific or the latest gem version and relate that to a specific rvm ruby that you have installed.
So if for example you have ruby-1.9.3-p327 and ruby-2.0.0-p247, you can have different gem version (or the same if you want) for each ruby version.

To upgrade to the latest one in the ruby-2.0.0-p247 for example you could do the following:

rvm list
rvm use ruby-1.9.3-p327
gem -version
1.8.17
rvm use ruby-2.0.0-p247
gem --version
1.8.17
gem update --system
gem --version
2.0.7
rvm use ruby-1.9.3-p327
gem --version
1.8.17

cannot load such file — zlib

Problemo

You are trying to install a gem with rvm but you are getting the following error:

ERROR: Loading command: install (LoadError
cannot load such file — zlib
ERROR: While executing gem … (NameError)
uninitialized constant Gem::Commands::InstallCommand

Solution

Following from the post here

you would need to install the zlib library with rvm, uninstall your ruby version and then install the ruby version again with the folowing:

 

rvm pkg install zlib

rvm uninstall 1.9.3

rvm install 1.9.3

Using named gemsets with rvm

Problem

You want to be able to use different gems/rails version for your application after installing the latest rails version, without having to use bundle exec.

 

Solution

You can use the named gemsets with rvm.

More instructions are here: http://beginrescueend.com/gemsets/basics/

so you can install two gemsets for example ruby-1.9.3-p0@rails3_0_11 and another with ruby-1.9.3-p0@rails3_2_1

you could then do :

rvm gemset create rails3_0_11 rails3_2_1

rvm 1.9.3-p0@rails3_0_11
gem install rails -v 3.0.11

rvm 1.9.3-p0@rails3_2_1
gem install rails -v 3.2.1

rvm gemset use rails3_0_11
bundle install

you could then also use some aliases in your ~/.bashrc file to be able to use the gemsets by using only one command as for example in ($ rvm3011):

# Rails 3.0.x
alias rvm3011="rvm gemset use rails3_0_11"
alias rvm3012="rvm gemset use rails3_0_12"

# Rails 3.2.x
alias rvm321="rvm gemset use rails3_2_1"
alias rvm322="rvm gemset use rails3_2_2"