Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous suggestions (#permit, #format_all, #safe_parse, …) #40

Open
csuhta opened this issue Feb 16, 2019 · 0 comments
Open

Miscellaneous suggestions (#permit, #format_all, #safe_parse, …) #40

csuhta opened this issue Feb 16, 2019 · 0 comments

Comments

@csuhta
Copy link

csuhta commented Feb 16, 2019

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant