Since our users have been complaining about putting "1 234" in a form
and having 1 stored instead of 1234 (because of how to_i/to_f works),
I made a simple monkeypatch to get their expected behavior:
> Since our users have been complaining about putting "1 234" in a form
> and having 1 stored instead of 1234 (because of how to_i/to_f works),
> I made a simple monkeypatch to get their expected behavior:
Don't see this in core.
Looks like a custom numerification that makes sense in your
application. Both that one and Rails' make arbitrary assumptions about
what they tolerate, but Rails is coherent with Ruby and I think the
current behaviour is better because of that.
Custom rules, yours or others, can be programmed as needed.
Couldn't agree more. This isn't by any means a "feature" for Rails-
Core. Maybe
you should look at the attribute_normalizer plugin.
Cheers,
Amol Hatwar (rubygem)
> On Oct 26, 1:09 pm, Xavier Noria <f...@hashref.com> wrote:
>> Don't see this in core.
>>
>> Looks like a custom numerification that makes sense in your
>> application. Both that one and Rails' make arbitrary assumptions about
>> what they tolerate, but Rails is coherent with Ruby and I think the
>> current behaviour is better because of that.
>
> I wouldn't call it arbitrary. My proposed behavior is based on how end
> users realistically may input numbers, and it improves their
> experience without any added ambiguity that I can think of. Support
> for commas/periods as thousand separators *would* add ambiguity since
> that is locale-dependent, but to my knowledge spaces in numbers are
> unambiguous across locales.
I understand it is not arbitrary in the sense that it is based on the
feedback of some users of *your* application. I didn't mean it is a
blind customization, but that is as arbitrary a priori as removing any
spurious character.
I have applications where numeric input filters remove any \D that is
not a comma or a period becaue that make sense for them, so I
understand your motivation. Removing \D is as arbitrary as removing \s
in my view, and thus I prefer the programmer to do that, instead of
core to do that by default.
> I would like to see specific examples of why it might be a bad idea to
> remove the spaces. I'm hearing nays but I don't get what issues you
> see with this. I do see the benefits, though: more intuitive and human-
> friendly input handling.
Hey, this is not a back and white issue. You propose a patch in -core,
and you get feedback. My feedback is that I do not see it in core.
In my view being tolerant to spaces can make sense for some, but that means that
12 678
is goona be 12678 by default in Rails. I don't buy that's good.
I don't think that interpretation is any better than 12 a priori. So
albeit I understand your motivation, as I said, I think the current
default is just as valid as yours, and in addition coherent with what
a Ruby programmer would expect from a string to number conversion.
What we'd lose is consistency in our framework.
It would be an error to try to provide you with use-cases for why this
is a bad idea. To do so would miss a very important part of what
makes Rails (and ruby for that matter) great. The main reason it's a
bad idea is that it introduces an inconsistency into an otherwise
consistent framework and language.
One of the huge wins that Ruby has over other languages, and something
which Rails remains (mostly) true to, is that it's logically
consistent. One can learn the principles of ruby and proceed to make
accurate deductions on language structure and behavior based on those
principles. Rails is (mostly) the same (and is getting more
consistent as it matures).
I go further than Xavier who said that "12 678".to_i => 12 is "just
as valid as" => 12678. A space is not a number, no one can argue that
it is. Also, one could never deduce the behavior you're suggesting
Henrik. One would have to accept that "removing spaces from numbers
is just the way it is". Why a space and not a tab? Why a space and
not a comma? "It just is."
If you want a tool that covers this type of special use-case in the
core, then I'd suggest you try PHP - it's one of the things it does
really well. It encodes common use cases without any regard to
consistency.
Jason
> It seems we simply disagree about whether user experience
> trumps consistency.
I think we rather disagree on whether that gives a significant better
user experience.
For you it is clear it does, for me (and I guess a few others) the
patch just relaxes number input in one of a miriad ways. In particular
I don't see why given
12 678
you one should pick 12678. I don't see why you don't pick 1000 out of
$1000
Wouldn't that be useful? What would a user could possibly mean other
than 1000? One could ask.
Given that I think the weakness in the patch is as arbitrary as any
other numerification that accepts strings that do not contain
well-formed numbers. That is, everything being equal (again, in my
opinion), I chose the behaviour that is consistent with Ruby. Because
everything being equal we get *in addition* expected behaviour from
for the programmer. And in any case it is configurable if an
application has custom rules.
I understand you disagree, that's OK :).
What I really think is that the current behaviour would also be
unacceptable... I would prefer an exception to be generated when
converting "12 34" to integer, instead of a result of 12 that would be
inconsistent with the user input...
Maybe ActiveRecord should generate automatically a
validates_numericality_of for each numeric field and include options for
:only_integer for integer fields. When a user explicitly set the
validation, it should override the automatic one...
That would be much more consistent in my opinion, regarding user's input...
Rodrigo.
Agreed. It's also supposed to be validating numericality, so if there
is a character in there which is not a number (a space) it should not
validate successfully.
All the posts to this point have suggested that a space is not part of
a numeric, so it would seem to make sense to back that claim up by
making it not validate.
TX