diff --git a/lib/money/currency.rb b/lib/money/currency.rb index f8b2a1d5df..db4f7062ef 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -83,8 +83,8 @@ def find(id) def find_by_iso_numeric(num) num = num.to_s.rjust(3, '0') return if num.empty? - id, _ = self.table.find { |key, currency| currency[:iso_numeric] == num } - new(id) + id = iso_numeric_index[num] + new(id) if id rescue UnknownCurrency nil end @@ -215,6 +215,12 @@ def reset! end private + + def iso_numeric_index + @iso_numeric_index ||= table.each_with_object({}) do |(id, attrs), index| + index[attrs[:iso_numeric]] = id + end + end def stringify_keys table.keys.each_with_object(Set.new) { |k, set| set.add(k.to_s.downcase) }