Example

wiseleyb Apr 14, 2011

Chops the last character off a string.

> a = "12345"
> a.chop
=> "1234"
> a
=> "12345"
> a.chop!
=> "1234"
> a
=> "1234

Why gsub!

benissimo Apr 14, 2011

I notice other adapters use sql.sub!, not sql.gsub! and in fact I had trouble with adding the limit parameter to any query involving nested selects. Replacing sql.gsub! with sql.sub! solved that problem for me. Has anyone else had a similar experience with this method?

In other words, replace:...

Working with match captures

wiseleyb Apr 4, 2011 2 thanks

Let's say you wanted to filter out passwords from:

s = "password=bob&password=jim&password=jane"

You'd do this:

r = /password\\=([^\\&]+)/
s.gsub!(r) { |m| m.gsub!($1, "[FILTERED]") }

Which would return

password=[FILTERED]&password=[FILTERED]&password=[FILTERED]

Handling nested hashes and arrays

CraigBuchek Mar 23, 2011 1 thank

You can use this code to handle nested hashes and arrays. I'm not sure if it handles every case, and it could probably be refactored better, but it's working quite well for us.

require 'active_support/core_ext/hash'

def normalize_params(params, key=nil)
params = params.flatten_keys if...

Don't cache it!

leente Mar 15, 2011

Don't store a connection in a variable, because another thread might try to use it when it's already checked back in into the connection pool. See: ActiveRecord::ConnectionAdapters::ConnectionPool

connection = ActiveRecord::Base.connection

threads = (1..100).map do
  Thread.new do...