Make timestamp of dmesg in Ubuntu human readable

dmesg shows the kernel ring buffer containing low-level system messages.
Per default, dmesg shows a timestamp:

12:59:26 fnordomator ~ > dmesg | tail
[101925.211846] usb 2-1.1: USB disconnect, device number 16
[110486.855788] usb 2-1.1: new high-speed USB device number 17 using ehci_hcd

If you're a human, use dmesg -T to print the timestamp human readable:

12:59:31 fnordomator ~ > dmesg -T | tail
[Di Apr 21 12:43:16 2015] usb 2-1.1: USB disconnect, device number 16
[Di Apr 21 15:05:57 2015] usb 2-1.1: new hig...

Unfreeze a frozen ActiveRecord

You can freeze any Ruby object to prevent further modification.

If you freeze an ActiveRecord and try to set an attribute you will an error like this:

can't modify frozen hash

This is because ActiveRecord delegates #freeze to its attributes hash.

You can unfreeze most Ruby objects by creating a shallow copy of the frozen object by calling #dup on it:

user = User.find(3)
user.freeze
unfrozen_user = user.dup

Notes for Rails 2 users
-----------------...

Make "rake notes" learn about Haml, Sass, CoffeeScript, and other file types

Rails comes with a Rake task notes that shows code comments that start with "TODO", "FIXME", or "OPTIMIZE".

While it's generally not good practice to leave them in your code (your work is not done until it's done), in larger projects you will occasionally have to use them as other parts of the application that you depend upon are not yet available.
To keep track of them, run rake notes. Its output looks something like this:

$ rake notes
app/controllers/fron...

Ruby: How to grow or shrink an array to a given size

If you want to grow a Ruby Array, you might find out about #fill but it is not really what you are looking for. [1]
For arrays of unknown size that you want to grow or shrink to a fixed size, you need to define something yourself. Like the following.

Array.class_eval do

  def in_size(expected_size, fill_with = nil)
    sized = self[0, expected_size]
    sized << fill_with while sized.size < expected_size
    sized
  end

end

Use it like this:

>> [1, 2, 3].in_size(5)
=> [1, 2, 3, nil, nil]

...

Rubymine: Code folding

Code folding is a very useful feature to me. It gives me a quick overview over a file and keeps me from scolling like a hamster in its wheel.

Keyboard shortcuts:

Collapse/expand current code block

strg -/+

Collapse/expand the whole file

strg ctrl -/+

When diving into Cucumber features or huge Ruby classes, I usually collapse all and the gradually expand what I need.

Using form models (aka decorators) with Devise

To use a form model with devise, you can simply override #resource_class in a controller. A typical use case would be the registrations controller, as users will need some fields only on sign-up. Example:

class Frontend::Authentication::RegistrationsController < Devise::RegistrationsController

  private

  def resource_class
    FrontendUser::AsSignUp # my decorator class, extending from FrontendUser
  end

end

Browse Amazon S3 buckets with Ubuntu Linux

There are some frontends available, but they all suck, are no longer maintained or are hard to install.

As a surprisingly comfortable alternative I have found a command line tool s3cmd:

sudo apt-get install s3cmd

When you run s3cmd the first time it will ask you for your access key ID and secret access key. This information is cached somewhere so you only need to write them once. To reconfigure later, call s3cmd --configure.

Once you're done setting up, s3cmd gives you shell-like commands like s3cmd ls or `s3cmd del som...

rspec_candy 0.4.0 released

  • Now supports RSpec 3 and Rails 4
  • Drops support for state_machine, which has some issues with Rails 4 and isn't actively maintained

Add an alternative image source for broken images

Awesome hack by Tim VanFosson:

<img src="some.jpg" onerror="this.src='alternative.jpg'" />

ActiveRecord: Order a scope by descending value without writing SQL

Instead of this:

Image.order('images.created_at DESC')

You can write this:

Image.order(created_at: :desc)

Not only do you not have to write SQL, you also get qualified column names (created_at becomes images.created_at) for free.

Multiple order criteria

To add secondary order criteria, use a hash with multiple keys and :asc / :desc values:

Image.order(title: :asc, created_at: :desc)

PostgreSQL: How to change attributes of a timestamp

It's generally not trivial to change a datetime's seconds, minutes, etc in SQL. Here is how it works when speaking PostgreSQL.

Consider you have a timestamp column whose seconds you want to zero:

SELECT born_at FROM users;
       born_at
---------------------
 2015-05-01 13:37:42

You can the TO_CHAR function to convert date or time values into a string, and do your changes there:

SELECT TO_CHAR(born_at, 'YYYY-MM-DD HH24:MI:00') FROM users;
       to_char
---------------------
 2015-05-01 13:37:00

...

Rails has a built-in slug generator

Today I learned that Ruby on Rails has shipped with a built-in slug generator since Rails 2.2:

> "What Up Dog".parameterize
=> "what-up-dog" 

> "foo/bar".parameterize
=> "foo-bar" 

> "äöüß".parameterize
=> "aouss" 

Also see: Normalize characters in Ruby.

Custom loggers in Ruby and Rails

File logger

If you need to log to a file you can use Ruby's Logger class:

require 'logger'

log = Logger.new('log/mylog.log')
log.info 'Some information'
log.debug 'Debugging hints'
log.error StandardError.new('Something went wrong')

Logger does a number of things well:

  • Message type (info / debug / error) is logged
  • Log entries are timestamped
  • Writing log output is synchronized between threads
  • Logged errors are printed with full backtraces

If you don't like the output format, you can define a custom formatter.

I ha...

Dusen (>0.5) now with "exclude from search"

Dusen (our search gem) is now capable of excluding words, phrases and qualified fields from search.
E.g. search for

  • included -excluded
  • "search this" -"not that"
  • topic:"Yes" -topic:"No"

This will soon also work in makandra cards!

How to use CSS to rotate text by 90° in IE8 (and modern IEs)

If you want to rotate text, you can use CSS transforms in somewhat modern browsers to rotate the container element.
However, if you need to support IE8, transform is unavailable (if need only IE9 support, ignore the following and use -ms-transform).

Here is a solution that worked for me:

<div class="my-element">Hi!</div>

^

.my-element {
  display: inline-block;
  transform: rotate(90deg);
  -ms-writing-mode: tb-rl;
  -ms-transform: none;
}

This way, browsers will use CSS transforms when available -- w...

Linux: Kill a process matching a partial name

This is useful to kill processes like ruby my-script.rb:

pkill -f my-script.rb

With great power comes great responsibility.