Bits


Today-I-learned liked micro-posts. (may move to Posts)

On this page:


PG::ConnectionBad

On occasion the local postgres server doesn’t get turn off properly when restarting the computer after updating software. Usually when this happens we get a PG::ConnectionBad exception. If on a Mac do:

# On the command line
$ cd /usr/local/var/postgres/

# Look for FATAL: lock file "postmaster.pid" already exists
# HINT:  Is another postmaster running in data directory "/usr/local/var/postgres"?
$ less server.log

# Remove offender file
$ rm postmaster.pid

# Restart local postgres server (if necessary)
$ postgres -D /usr/local/var/postgres

On other systems such as GNU/Linux, most likely will find the offender file on /usr/var/postgres/.


Let Bundler Handle Autotest

To let Bundler handle Minitest-autotest just make sure to include gem minitest-autotest in the Gemfile. This will put the gem and its dependencies on the load path. That gets rid of the invalid option: --server exception you see otherwise.


Enumerable Shortcuts

You can call a method on every item of an enumerable like

[1, 2, 3].each { |num| num.to_s }

Which can be further reduced to:

[1, 2, 3].each(&:to_s)

Unfortunately this method doesn’t work if you want to achieve something like

%w[hi how are you].each { |word| puts word }

Instead, you would have to use something like:

%w[hi how are you].each(&method(:puts))

Beware that parenthesis are required around :puts. Also, this only works if each element is passed as the given method’s argument.


Minitest's refute

Minitest’s refute checks for ‘not true’ (falsy) rather than for false. If we want our tests to fail when nil comes around perhaps, rather than testing for: refute boolean? tests should check for both: refute boolean? and refute_nil boolean?

Both refutations could be replaced with assert_equal false, boolean?. In which case it would be better to use assert_equal true, boolean? rather than assert boolean? for consistency.


Array-argument & Splat Operator

If you use the splat operator to pass several parameters to a method, like so:

def add(*numbers)
  numbers.inject(0) { |sum, number| sum + number }
end

Then, you can pass an Array prepended by the splat operator to the method and it will work.

add *[1,2,3,4] # => 10

some_numbers = [4, 6, 7, 8, 10]
add *some_numbers # => 35

Clean up Homebrew

Ever wonder how much space are old homebrew bottles taking from your Mac? Simply call brew cleanup -n from the terminal and you’ll get the list of bottles that would be removed as well as an approximation of how much space would be free. If you actually want to remove them simply call brew cleanup.


Better Ruby random numbers

rand might be enough for generating arbitrary things. If you need to generate a random number for something like a security token use the Secure Random library. It’s part of the Ruby standard library so there’s no need for gems.

require 'securerandom'

SecureRandom.hex  # => "3b5db98fa369b52545b626040403feb7"