Permalink
Browse files

refactor a couple injects to map + Hash[]

  • Loading branch information...
tenderlove committed Sep 8, 2010
1 parent 1276c28 commit a746090a31040de7d055ee0915535525770e9856
Showing with 2 additions and 5 deletions.
  1. +2 −5 activerecord/lib/active_record/base.rb
@@ -684,7 +684,7 @@ def columns
# Returns a hash of column objects for the table associated with this class.
def columns_hash
- @columns_hash ||= columns.inject({}) { |hash, column| hash[column.name] = column; hash }
+ @columns_hash ||= Hash[columns.map { |column| [column.name, column] }]
end
# Returns an array of column names as strings.
@@ -1813,10 +1813,7 @@ def comma_pair_list(hash)
end
def quote_columns(quoter, hash)
- hash.inject({}) do |quoted, (name, value)|
- quoted[quoter.quote_column_name(name)] = value
- quoted
- end
+ Hash[hash.map { |name, value| [quoter.quote_column_name(name), value] }]
end
def quoted_comma_pair_list(quoter, hash)

5 comments on commit a746090

huned replied Nov 2, 2010

I'm curious... is this a stylistic change or is Hash[] better than Enumerable#inject in some way I don't know?

Contributor

josevalim replied Nov 2, 2010

It is considerably faster.

huned replied Nov 2, 2010

And now I know. Thanks Jose.

Contributor

asanghi replied Nov 3, 2010

Jose, I use index_by in my code quite a bit, do you think that should also be modified to

def index_by
  Hash[map{|element| [yield(element), element] }]
end
Contributor

asanghi replied Nov 3, 2010

..and egg on face, it's already done on master (but not on 3-0-stable). Sorry for invading your mailboxes unnecessarily.

Please sign in to comment.