Using Array.wrap in Rails when the results could either be a string or an array

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You want to use some array method in a value returned, which can either be a single value or an array of values.

Solution

Rails ActiveSupport Array, provides a method called wrap, that can be used to:

Wraps its argument in an array unless it is already an array (or array-like)

Full explanation of the method here.

Thanks Miles

Capturing output with UnitTest in Ruby

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You have some ruby code, a rake task for example, that outputs some results in the standard output, but you would like to test it in your unit tests.
MiniTest has capture_io and capture_subprocess_io, but there is nothing similar in UnitTest.

Solution

Looking at the code of the above MiniTest assertions you can create your own assertion to be used in your tests.

Create a new file under your test folder called MyAssertions and use the following code (taken from MiniTest), for your capture_output assertion:

Then include it in your test_helper.rb file with:

and use it in your tests like:

How to display the git last commit of every file in a list with ruby

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You want to display the last commit of every file that you have in a list.

Solution

Let’s say that you have the following file list in an array:

in order to be able to get the last commits of each of the files in ruby do the following:

Could not find libv8-3.16.14.9 in any of the sources

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You are trying to upgrade one of your gems (ie uglifier) and at the same time you are using a new gemset (ie for rails 4.2.4), and you get the following error when you are trying to bundle:

Solution

Upgrade to rails 4.2.4 first and bundle and afterwards change the other gem and do bundle again.

OSVDB 119927 : http Gem for Ruby SSL Certificate Validation MitM Spoofing

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

There was a security vulnerability issued by Gemnasium about the http gem with the title OSVDB-119927 – MitM Security Vulnerability.

The details for it are here.

Solution

After some investigation (gem dependency http –reverse-dependencies) it turns out that the twitter gem (5.14.), is using an older vulnerable http dependency (0.6.3).

In order to remove this warning and until there is a new twitter gem released, you can use the github master branch of twitter, like:

rvm – Branch origin/ruby_2_1_0 not found.

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

You are trying to install the new ruby in your ubuntu rvm installation with rvm install 2.1 but you are getting the error:

Solution

If you are careful and you notice a warning message as:

then you would need to update your rvm installation to the latest one with:

And then you should be able to install the latest ruby version with:

Dreamhost keeps asking for password when installing rvm ruby version

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

Problem

After installing rvm on dreamhost, when trying to install any ruby version afterwards, there is a prompt for password, and when entering the user’s password there is the message that the user is not in the sudoers list.

Solution

To be able to install the rvm ruby version you would like you should be installing them by using the following which is based on the answer here :

and then:

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

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

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.