rvm single user installation fails with: curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

Problem

You want to install rvm as a single user in a new linux but when trying to install it with the normal installation instruction:

curl -sSL https://get.rvm.io | bash -s stable --ruby

it fails with the following error:

curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

Solution

You are behind a proxy server and you need to setup your environment variables for the server in your .profile file like the following (and make sure you source the file afterwards with . ~/.profile):

export http_proxy=http://xxx.xxx.xxx.xxx:port_no/
export https_proxy=https://xxx.xxx.xxx.xxx:port_no/

Replacing spaces with underscores in linux busybox

Problem

You want to replace the spaces in filenames (ie in Brennan’s B2 system), that uses busybox and you do not have all the linux commands available.

Solution

Use the following in the directory with the files (ie flac files). At the moment this command only works in the directory that is being run.

# find . -type f -name "*.flac" -exec ash -c 'mv "$0" "${0// /_}"' '{}' ';'

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.