Split an array into columns

You know that you can collect an array as groups using in_groups or in_groups_of.

Maybe you want to fetch those values in "columns" where the first value lives in the first column, the second one in the second, etc. until it wraps, so that for example the fourth value is in the first of three columns.

Put the attached file into config/initializers/ to be able to say in_columns on any Array:

>> [1, 2, 3, 4, 5, 6, 7].in_columns(3)
=> [[1, 4, 7], [2, 5], [...

Setting up Ubuntu Server as Virtual Machine in VMware

Install Ubuntu Server

  • Download an image from this site
  • Select File > New… to and follow the instructions, choose the .iso file you downloaded as image file
  • Deselect "Simple installation" – you want to configure your system yourself
  • Start the new virtual machine and follow the instructions

Install VMware Tools

  • Choose Virtual Machine > Install VMware Tools from the VMware menu, then:

^
# install required packages
sudo apt-get install build-essential psmisc

# m...

Bash script to run specs and features

Run rspec-and-cucumber from any project directory to run both RSpec and Cucumber. If available, rspec_spinner or cucumber_spinner are used.

Note that features are not run when specs fail.\
If you prefer to run them in parallel or run features regardless of the spec results, please adjust it for yourself accordingly.


This script is part of our geordi gem on github.

Nautilus File Manager slows down the system after new installation of ubuntu

After a new system installation, Ubuntu One starts automatically and seems to try to sync your complete home directory. This slows down the Nautilus File Manager extremely. To prevent this, uncheck Ubuntu One from your start programs and restart the system.

Change the hostname on Mac OS X

To set the hostname of your Mac, run the following command in Terminal:
sudo scutil --set HostName my-new-hostname.local

Check it by typing:
hostname

Using QEMU to quickly test an ISO or bootable USB drive

When you want to quickly boot from a drive or image in a virtual machine you do not need to setup up a VirtualBox machine. Often QEMU does the job well enough.

Install it:

sudo apt-get install qemu

To boot an ISO:

qemu-system-x86_64 -cdrom filename.iso

If you prepared a USB pen drive and want to test it, run it like this (/dev/sdx being your device name; you may need to sudo to access it):

qemu-system-x86_64 -hda /dev/sdx

Be aware that not everything runs smoothly in QEMU -- you might need to set up a _VirtualBox...

Vector Magic: Precision Bitmap To Vector Conversion Online

Automatically convert bitmap images like JPEGs, GIFs and PNGs to the crisp, clean, scalable vector art of EPS, SVG, and PDF with the world's best auto-tracing software.

Playing audio in a browser

If you want to play music or sounds from a browser, your choice is to use either Flash or the new <audio> tag in HTML5. Each method has issues, but depending on your requirements you might not care about all of them.

Flash

  • Works in all desktop browsers, even Internet Explorer. Does not work on iPads or iPhones.
  • Requires you to embed a Flash component into your page which will later play the audio for you.
  • Can play MP3s or Wave files. Cannot play OGG Vorbis audio.
  • Cannot reliably seek to a given position when playing VBR-enco...

Print-Friendly Images and Logos with CSS

The trick is this: send a low-resolution version of your image to the screen, and a high-resolution version to the printer.

Git Cheatsheet

Make sure you understand differences between git's areas (such as stash, workspace, upstream, etc.) and what commands affect which areas.

javan/whenever - GitHub

Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.

Validations on associated objects

Validations that need to access an associated object may lead to some trouble. Let's exemplify that using this example:

class Project < ActiveRecord::Base
   has_one :note
end

class Note < ActiveRecord::Base
   belongs_to :project
end

Now say we need a validation that ensures that a description is set (validates_presence_of :description) within Note but only if the Project has a flag called external set to true. The following code will lead to some problems as the associated object is not present when creatin...

Speed up Capistrano deployments using a remote cached copy of repository

You can seriously speed up deployments with Capistrano when using a local git repository on the server you are deploying to.

Simply add

set :deploy_via, :remote_cache
set :copy_exclude, [ '.git' ]

to your config/deploy.rb and Capistrano will create a clone in shared/cached-copy. This will be updated using git pull when deploying which transfers less bytes and is usually much faster. If deploy_via is set to use default settings (being "export"), Capistrano will do a full clone of the repository from your git host otherwi...

Apprise - The attractive alert alternative for jQuery

An alert alternative for jQuery that looks good. Apprise is a very simple, fast, attractive, and unobtrusive way to communicate with your users. Also, this gives you complete control over style, content, position, and functionality. Apprise is, more or less, for the developer who wants an attractive alert or dialog box without having to download a massive UI framework.

Capistrano 2: Which Capistrano hooks to use for events to happen on both "cap deploy" and "cap deploy:migrations"

When deploying an application with "cap deploy" by default [1] you only deploy your code but do not run migrations. To avoid an application to be running with code which requires database changes that did not happen yet you should use cap deploy:migrations.

The problem

Let's say that you have something like that in your config/deploy.rb to create a database dump every time you deploy:
before 'deploy', 'db:dump'
This will not be called for cap deploy:migrations. The same applies to other things that are hooked s...

Why a Rails flash message is shown twice

You set a flash message and it shows up as it should. However, it is displayed again the next time you follow a link. Here is why:

You have to differentiate between a render and a redirect_to because a flash message is only deleted after a redirect. If you want a message to be seen in the next request after a redirect, use flash[]. If you want a message to be seen in the current request, use flash.now[].

Workaround for the lazy

If you cannot be bothered to decide which flash hash to use, or if the fl...

Ignore an error class with Airbrake

Airbrake (formerly Hoptoad) already ignores certain errors like InvalidAuthenticityToken by default (see Airbrake::Configuration::IGNORE_DEFAULT).\
To ignore additional classes of errors, put this into config/initializer/airbrake.rb:

Airbrake.configure do |config|
  config.ignore << 'ActiveRecord::IgnoreThisError'
end

You probably also want to ignore ActionController::MethodNotAllowed and ActionController::UnknownHttpMethod exceptions.

See the github page for more options on h...

Haml and Sass 3.1 are Released

Sass now comes with user-defined functions, keyword arguments, list manipulation. Haml and Sass are now two separate gems.

HTML5 Presentation

Awesome presentation for the new HTML5 features we will get to play with. This presentation should probably be viewed in Chrome only.

How to translate “business value” of things that are technically important

User Stories should describe what a user wants the system to do. Purely technical tasks should usually be implemented as part of a User Story. But, sometimes there are technical tasks which cannot be directly linked to customer value. Things like “Upgrade to MySQL 6.0″ or “replace magic numbers with enums” need to be done. How can you prioritize these critical chores against User Stories? How can you make the product owner aware of the importance of such tasks (and the business risks of procrastination)?

Rails jQuery UJS: Now Interactive

We can now plug into every facet of the Rails jQuery UJS adapter, binding to custom events, and even customizing internal functions, without hacking or monkey-patching the rails.js file itself.

Saving application objects in your session will come back to haunt you

If you save a non-standard object (not a String or Fixnum, etc) like the AwesomeClass from your application in the session of visitors be prepared that some time you will get this exception:

ActionController::SessionRestoreError: Session contains objects whose class definition isn't available. Remember to require the classes for all objects kept in the session. (Original exception: ...)

This happens when you remove your AwesomeClass but users come back to your site and still have the serialization of such objects in their session....

How to: Apache logs on a daily basis without logrotate

If you want to have a new log file every day automatically, but avoid using logrotate, the CustomLog directive is your friend:

CustomLog "|/usr/sbin/rotatelogs /opt/www/awesome-project/log/access.%Y-%m-%d.log 86400" combined

Adding that to your site's vhost will create log files that include the current date in their name, like access.2011-04-20.log, without any need to restart the web server every night (like logrotate does).

The last argument above is the rotation time in seconds -- here being 86400 (= 60 * 60 * 24) which ca...

How to fix strangely disappearing or misbehaving forms

You most likely have a form element inside another form element. Don't do that. Ever.

Firefox and Chrome will discard the first form nested inside another form (but for some reason keep others). Internet Explorer will possibly act like nothing is wrong -- but break (send the outer form) when you submit.

If your application behaves normal at first but removes forms from the DOM when you Ajax around, this could be the cause. Remember this note when you think your browsers are broken once again and check for such things thoroughly bef...