You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the response, and the context with regard to the previous issue. That's helpful.
I think it's a regression, primarily because it doesn't respect the allow_empty: false option. Which in the previous version would return nil instead of an empty string.
Secondly, it leads to unexpected behavior. Which in turn can be challenging to debug. Let me explain:
An often preferred coding style is using coercion over type checking. Which is why it'd be somewhat common to coerce the given value to the expected type, which is how we end up passing nil.to_s as the string argument.
Showing an example, I would expect these to behave similar, since they are functionally the same:
But as you can see, the results are different. And confusing. Since many devs may not know that nil.to_s returns a frozen string or why that would matter.
It does make sense to return the given value instead of nil. However, that breaks down when used in conjunction with the allow_empty flag, which explicitly alters this behavior. That's it's entire purpose.
Leading to this confusing behavior:
StripAttributes.strip("",allow_empty: false)# => nil <-- Correct. The flag specifies no empty valuesStripAttributes.strip(nil.to_s,allow_empty: false)# => "" <-- Wrong. I specified "no empty values". this should be nil
Whether the empty string is frozen or not, doesn't matter here. It's essentially a noop. But the return value is not correct.
We need to apply the same logic as we do at the bottom of the method.
I can open a PR to update it, if you're on the same page.
This snippet should return
nil
, since its given a blank string andallow_empty: false
The reason is this guard against frozen strings:
https://github.com/rmm5t/strip_attributes/blob/master/lib/strip_attributes.rb#L55
The text was updated successfully, but these errors were encountered: