rails/rails — pull request #16833 by sferik (2014-09-07 08:40:49+00:00)
Title: Pass symbol as an argument instead of a block

Using Ruby’s `Symbol#to_proc` method is considerably more concise than creating a new block, not to mention about 20% faster (see benchmark below). `Symbol#to_proc` is already used in many places throughout the Rails codebase, but not everywhere. This patch makes it used everywhere possible. In some cases, the actually reduces the number of lines of code. For example, in `railties/lib/rails/application/routes_reloader.rb`

```ruby
def finalize!
route_sets.each do |routes|
routes.finalize!
end
end
```

Becomes:

```ruby
def finalize!
route_sets.each(&:finalize!)
end
```

###### Benchmark

```ruby
require 'benchmark/ips'

def fast
(1..100).map{|i| i.to_s}
end

def slow
(1..100).map(&:to_s)
end

Benchmark.ips do |x|
x.report("slow") { fast }
x.report("fast") { slow }
end
```

```
slow 46707.3 (±7.8%) i/s - 235768 in 5.079617s
fast 55143.5 (±7.2%) i/s - 276554 in 5.040752s
```