jlogsdon/acts_as_approvable — pull request #2 by hc5duke (2012-01-26 23:38:08+00:00)
Title: Fix bug

for lines 58-61 of `approval.rb` is wrong, because:

```ruby
Hash['a', 1, 'b', 2] # => { 'a' => 1, 'b' => 2 }
Hash[['a', 1, 'b', 2]] # => {}
```

Another way would be:

```ruby
data = Hash[*object.to_a.select do |attr_value|
item.attributes.include?(attr_value.first)
end.flatten]
```

but that seemed too fancy, and also slower:

```ruby
n = 50_000
object = { 'a' => 1, 'c' => 3 }
attributes = { 'a' => 2, 'b' => 4 }

Benchmark.bm(6) do |b|
b.report 'fancy' do
n.times do
data = Hash[*object.to_a.select do |attr_value|
attributes.include?(attr_value.first)
end.flatten]
end
end

b.report 'boring' do
n.times do
data = {}
object.each do |attr, value|
data[attr] = value if attributes.include?(attr)
end
end
end
end
```

returns

```
user system total real
fancy 0.620000 0.000000 0.620000 ( 0.623037)
boring 0.190000 0.000000 0.190000 ( 0.187836)
```