powerpack
powerpack copied to clipboard
Miscellaneous suggestions (#permit, #format_all, #safe_parse, …)
These are some methods I've personally added on projects before, I don't have time to PR them, but you might find these interesting ideas:
# Returns `value` if `value` is in the set or array
# otherwise returns the `default` (which is nil unless provided)
# These methods are useful for sanitizing input or expected values
array.permit(value, default)
set.permit(value, default)
# Opposite of Object#is_a?
object.not_a?(klass)
# Opposite of Object#nil?
object.not_nil?
# Like Time.parse and URI.parse, but returns nil instead of raising an exception
# when the input is invalid
Time.safe_parse(a_string)
URI.safe_parse(a_string)
# Like nil.to_i, but for BigDecimal
# Returns a 0.0 BigDecimal
nil.to_d
# Applies Kernel.format to every value of the array using `format_string`
array.format_all(format_string)