mysql2 and older ruby versions

The mysql2 gem in version 0.3.13 might break while compiling on older patch releases of Ruby 1.9.3 within rvm:

*** [err :: server] ruby: symbol lookup error: /path/to/deployment/shared/bundle/ruby/1.9.1/gems/mysql2-0.3.13/lib/mysql2/mysql2.so: undefined symbol: rb_wait_for_single_fd
*** [err :: server] ruby: symbol lookup error: /path/to/deployment/shared/bundle/ruby/1.9.1/gems/mysql2-0.3.13/lib/mysql2/mysql2.so: undefined symbol: rb_wait_for_single_fd

Fixating mysql2 to version 0.3.11 helped.

dusen and edge_rider gems no longer depend on Rails

dusen 0.4.8 and edge_rider 0.2.3 no longer depend on Rails (they still depend on ActiveRecord). That means you can use them e.g. with Sinatra.

.rvmrc deprecated in favor of .ruby-version and .ruby-gemset

Do not use .rvmrc files to specify Ruby version and gemset configuration any longer, it's deprecated and not considered by other Ruby version managers such as rbenv.

If you want to migrate an existing .rvmrc you can use rvm rvmrc to .ruby-version.
Put gemset specification into .ruby-gemset.

Creating the .ruby-version file on your own, just make a file containing e.g.

1.8.7

Attention: Don't clutter other developers rvms with several unecessary ruby patch levels

When you use the rvm command

rvm --ruby-version use 1....

"cannot load such file -- nokogiri/nokogiri" (or any other Gem with native extensions on rvm and Ruby >= 2)

After running bundler / gem install I could not load nokogiri lately. It died with cannot load such file -- nokogiri/nokogiri.
This is not a problem of the gem but is due to a broken native extensions installation routine.

When installing nokogiri manually and with verbose output by using gem install -V nokogiri -v 1.5.6, you can see the problem scrolling by when the native extension is built:

/usr/bin//install -c -m 0755 nokogiri.so /home/thomas/.rvm/gems/ruby-2.0.0-p247/gems/nokogiri-1.5.6/lib/home/thomas/.rvm/rubies/ruby-2.0.0-p...

Use bundle open to open a gem's code in your $EDITOR

bundle open BUNDLED_GEM will open the BUNDLED_GEM's source code in your default editor.

Parallel gem installing using Bundler

Bundler 1.4.0 (still beta) can install gems in parallel, making a run of bundle install much faster.

The "private" modifier does not apply to class methods or define_method

Ruby's private keyword might do a lot less than you think.

"private" does not apply to class methods defined on self

This does not make anything private:

class Foo

  private

  def self.foo
    'foo'
  end
  
end

You need to use private_class_method instead:

class Foo

  def self.foo
    'foo'
  end
  
  private_class_method :foo
  
end

"private" does not apply to define_method

This does not make anythin...

RubyLTS

RubyLTS is a long term supported fork of Ruby 1.8 that will continue to receive security updates for the forseeable future.

exception_notification 4.0.0+ makes it easier to ignore errors, crawlers

The new exception_notification has awesome options like :ignore_crawlers => true and :ignore_if => lambda { ... }. These options should be helpful in ensuring every notifications means something actionable (instead of a long log of failures that just scrolls by).

Note that you should not ignore crawlers by default. Ideally, cool URLs never change and always respond with a helpful redirect or similar.

Ignore Errors like this:

# config/initializers/exception_notification.rb

Ex...

Consul 0.9 lets you optimize records checks

Consul 0.9 comes with many new features to optimize powers that only check access to a given record. e.g. Power.current.post?(Post.last). See below for details.

Powers that only check a given object

Sometimes it is not convenient to define powers as a collection. Sometimes you only want to store a method that
checks whether a given object is accessible.

To do so, simply define a power that ends in a question mark:

class Power
  ...

  power :upd...

Cryptic Ruby Global Variables and Their Meanings

The linked page lists and explains global Ruby "dollar" variables, such as:

  • $: (load path)
  • $* (ARGV)
  • $? (Last exit status)
  • $$ (PID)
  • $~ (MatchData from last successful match)
  • ...and many more you'll need when reading weird code.

Regex

  • $~ (last MatchData)
  • $1 $2 $3 $4 (match groups from the last pattern match)
  • $& (last matched string)
  • $+ (last match group)
  • `$`` (the string before the last match)
  • $' (the string after the last match

See [this extensive list of variables](http://www.tu...

Short lambda syntax in Ruby 1.9

Ruby 1.9 brings a shorter way to define lambdas using the -> operator:

twice = -> (x) { 2 * x }
twice.call(5) # => 10

This is equivalent to:

twice = lambda {|x| 2 * x }
twice.call(5) # => 10

Note that the syntax is subtly different from Coffeescript where you define function parameters before the arrow: (x) -> { 2 * x }.

Sprites with Compass

Using CSS sprites for background images is a technique for optimizing page load time by combining smaller images into a larger image sprite.

There are ongoing arguments on how useful this still is, as modern browsers become more comfortable to load images in parallel. However, many major websites still use them, for example amazon, [facebook](...

Live CSS / view reloading

Next time you have to do more than trivial CSS changes on a project, you probably want to have live CSS reloading, so every time you safe your css, the browser updates automatically. It's pretty easy to set up and will safe you a lot of time in the long run. It will also instantly reload changes to your html views.

Simply follow the instructions below, taken from blog.55minutes.com.

Install CSS live reload (only once per project)

  1. Add th...

Batch-process text files with ruby

Did you know you can do in-place batch processing with plain ruby?

The following script will in-place replace "foo" with "bar" in all files you feed it. Call it with ./my_script path/to/my/files/*

#!ruby -i -p
$_.gsub!(/foo/, "bar")

"'-i -p" means:

Ruby will run your script once for each line in each file. The line will be placed in $_. The value of $_ at the end of your script will be written back to the file.

Shorter

Using the Kernel#gsub shorthand for $_.gsub!:

#!ruby -i -p
gsub(/foo/, "bar")

S...

Upgrading Rails 2 from 2.3.8 through 2.3.18 to Rails LTS

This card shows how to upgrade a Rails 2 application from Rails 2.3.8 through every single patch level up to 2.3.18, and then, hopefully, Rails LTS.

2.3.8 to 2.3.9

This release has many minor changes and fixes to prepare your application for Rails 3.

Step-by-step upgrade instructions:

  1. Upgrade rails gem
  2. Change your environment.rb so it says RAILS_GEM_VERSION = '2.3.9'
  3. Change your ...

Rails: Have different session secrets for all environments

The Rails secret_token must be unique for each application and any instance of it. If not, someone could exploit this by creating a user with ID = 1 (e.g. on staging), sign in and then use that cookie to authenticate on another site (e.g. on production, where the user with ID = 1 probably is the admin).

Here is a one-for-all solution that does not affect current production users, leaving the production token unchanged: prefix the existing secret_token with #{Rails.env unless Rails.env.production?}.

Note: There may be tokens in ...

Before you make a merge request: Checklist for common mistakes

Merge requests are often rejected for similar reasons.

To avoid this, before you send a merge request, please confirm that your code ...

Virtus: Coercing boolean attributes

TLDR

Do it like this:

attribute :active, Virtus::Attribute::Boolean

Long story

In Virtus you define attribute with their type like this:

attribute :name, String
attribute :birthday, Date

When defining a boolean attributes, you will probably write it like this:

attribute :active, Boolean

The problem is, there is not actually a Boolean class in Ruby (there's only TrueClass and FalseClass), so use Virtus::Attribute::Boolean instead.

The reason whil...

marcandre/backports ยท GitHub

Essential backports that enable many of the nice features of Ruby 1.8.7 up to 2.0.0 for earlier versions.

Subscribe to Rails security mailing list without Google account

The Ruby on Rails security list archive can be found here: http://groups.google.com/group/rubyonrails-security

You can subscribe to this mailing list without a Google account by pasting this URL into your browser (after replacing the email address obviously).

http://groups.google.com/group/rubyonrails-security/boxsubscribe?email=your.name@example.com
                                                                       ^^^^^^^^^^^^^^^^^^^^^ <- Change this

Ruby Scripts: Select the Ruby version in the shebang

As Bill Dueber has on his blog, you can call rvm in the shebang to select a Ruby version like this:

 #!/usr/bin/env rvm 1.9 do ruby

Standard arguments to do apply, see $> rvm help do.

Using Thin for development (with SSL)

Note: These instructions are for a quick per-project setup and may require you to change code. If you generally need SSL for development, you probably want to use Passenger.


  1. Create a directory .ssl in your home directory. Go there and create a self-signed certificate. It is important to enter localhost.ssl as Common Name when asked. This is to mak...

Detect city, country from IP address

  • You can detect city and country from an IP address by using the GeoLite database. This is a flat file you can copy into your project (~ 20 MB).
  • You can access the database using the geoip gem.
  • You need to attribute MaxMind if you are using the data.
  • Accuracy sort of sucks. For most countries 1/3 of addresses cannot be resolved within 40 kilometers, probably because the Inter...