Error installing json 1.8.3 with ruby 2.4

Problem

You are trying to install the gems for your rails application in a new computer, or by using ruby 2.4 and you get the following error:

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

Solution

Remove your old Gemfile.lock file and run bundle again. A newer version of json (ie 1.8.6) should be installed.

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

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

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:

module MyAssertions
  module CaptureOutput
    # Use as:
    # out = capture_output { method_to_call(param1, param2) }
    # output = out[0], error = out[1] or
    # output = out.first, error = out.last
    def capture_output
      require 'stringio'

      captured_stdout, captured_stderr = StringIO.new, StringIO.new
      orig_stdout, orig_stderr = $stdout, $stderr
      $stdout, $stderr         =  captured_stdout, captured_stderr

      begin
        yield
      ensure
        $stdout = orig_stdout
        $stderr = orig_stderr
      end
      return captured_stdout.string, captured_stderr.string
    end

    def capture_subprocess_output
      require 'tempfile'

      captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
      orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
      $stdout.reopen captured_stdout
      $stderr.reopen captured_stderr

      begin
        yield

        $stdout.rewind
        $stderr.rewind

        [captured_stdout.read, captured_stderr.read]
      ensure
        captured_stdout.unlink
        captured_stderr.unlink
        $stdout.reopen orig_stdout
        $stdout.reopen orig_stderr
      end
    end
  end

  include CaptureOutput
end 

Then include it in your test_helper.rb file with:

require './test/my_assertions.rb'

and use it in your tests like:

out = capture_output { method_called(prm1, prm2) }

assert_match(/required_value/, out.first)

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

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:

file_list = ["Gemfile", "Gemfile.lock", ".ruby-version"]

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

file_list.each do |f|
  puts %x[git log -n 1 #{f}]
end

Could not find libv8-3.16.14.9 in any of the sources

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:

Could not find libv8-3.16.14.9 in any of the sources

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

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:

gem 'twitter', github: 'sferik/twitter'

rvm – Branch origin/ruby_2_1_0 not found.

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:

Branch origin/ruby_2_1_0 not found.
There has been an error while checking out branch ruby_2_1_0.
Halting the installation.                               
There has been an error fetching the ruby interpreter. 
Halting the installation.

Solution

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

Warning, new version of rvm available '1.25.26', 
you are using older version '1.24.8'.

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

rvm update

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

rvm install 2.1

Dreamhost keeps asking for password when installing rvm ruby version

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 :

rvm list remote
ruby-1.9.3-p194
   ruby-1.9.3-p286
   ruby-1.9.3-p327
   ruby-1.9.3-p362
   ruby-1.9.3-p374
   ruby-1.9.3-p392
   ruby-1.9.3-p429
   ruby-1.9.3-p448
   ruby-2.0.0-p0
   ruby-2.0.0-p195
   ruby-2.0.0-p247

and then:

rvm mount -r https://rvm.io/binaries/debian/6/x86_64/ruby-2.0.0-p247.tar.bz2 --verify-downloads 1