Skip to content

Commit 4d3a2c0

Browse files
authored
Merge pull request rails#43106 from flavorjones/flavorjones-activesupport-number-converter-valid-float
Avoid use of exceptions to detect invalid floats
2 parents 835f4bf + 900ce92 commit 4d3a2c0

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Improves the performance of ActionView::Helpers::NumberHelper formatters by avoiding the use of
2+
exceptions as flow control.
3+
4+
*Mike Dalessio*
5+
16
* `preload_link_tag` properly inserts `as` attributes for files with `image` MIME types, such as JPG or SVG.
27

38
*Nate Berkopec*

actionview/lib/action_view/helpers/number_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,8 @@ def valid_float?(number)
448448
end
449449

450450
def parse_float(number, raise_error)
451-
Float(number)
452-
rescue ArgumentError, TypeError
453-
raise InvalidNumberError, number if raise_error
451+
result = Float(number, exception: false)
452+
raise InvalidNumberError, number if result.nil? && raise_error
454453
end
455454
end
456455
end

activesupport/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Improves the performance of ActiveSupport::NumberHelper formatters by avoiding the use of
2+
exceptions as flow control.
3+
4+
*Mike Dalessio*
5+
16
* Removed rescue block from `ActiveSupport::Cache::RedisCacheStore#handle_exception`
27

38
Previously, if you provided a `error_handler` to `redis_cache_store`, any errors thrown by

activesupport/lib/active_support/number_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ def number_to_phone(number, options = {})
9999
# number_to_currency(1234567890.506, locale: :fr) # => "1 234 567 890,51 €"
100100
# number_to_currency('123a456') # => "$123a456"
101101
#
102-
# number_to_currency("123a456", raise: true) # => InvalidNumberError
103-
#
104102
# number_to_currency(-0.456789, precision: 0)
105103
# # => "$0"
106104
# number_to_currency(-1234567890.50, negative_format: '(%u%n)')

activesupport/lib/active_support/number_helper/number_converter.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ def default_value(key)
174174
end
175175

176176
def valid_float?
177-
Float(number)
178-
rescue ArgumentError, TypeError
179-
false
177+
Float(number, exception: false)
180178
end
181179
end
182180
end

0 commit comments

Comments
 (0)