From: shevegen@... Date: 2016-04-24T19:01:26+00:00 Subject: [ruby-core:75166] [Ruby trunk Feature#12306] Implement String #blank? #present? and improve #strip and family to handle unicode Issue #12306 has been updated by Robert A. Heiler. Just one comment, not related to the suggestion itself but to one other comment made above. Richard Schneeman wrote: > I think this is useful outside of Rails. The Active Support module has > 87 million downloads on rubygems.org while Railties has only 53 million > downloads. So 34 million times people have needed to use Active Support > without Railties, this is a huge number. This is not a good comparison to show the "most downloads" statistics alone. Many downloads are bundled together with prior dependencies, e. g. rails. All the Active* gems are, in most cases, tied to the usage of rails. Most people who will do a "gem install rails" won't know what the individual active* gems do or how they work. Not everyone who uses ruby, also uses rails. And idioms that are available to ruby itself, should primarily make and see fit within ruby itself, not to external gems/projects. A lot of rails has also influenced ruby itself too, but rails is not ruby. String#blank? may fit well into the active* world but for a regular string object, it is not entirely clear what it should mean. Should it mean whether the String contains one or more "blanks"? In this case, it would have to check for at least one ' ' in that string object. But this is not what the method does. The documentation is: "A string is blank if it's empty or contains whitespaces only" So the name of the method is actually wrong in my opinion, since it also checks whether the string is empty. Is an empty string a blank string or does it include a "blank" string or character? In an empty string, there surely is no space character. It sure enough does not include any ' ' so why would it be considered blank? And for string objects such as: "This cat is a happy cat.".blank? This would return false in the above definition, but it sure enough does include the ' ' character. I assume the method .blank? here more does a .strip.empty? check combined. Calling this operation ".blank?" still does not seem to be right IMO. An advantage, and a huge point in favour of ninja patching (also called monkey patching), you model ruby within the particular domain thinking. (And with refinements, perhaps one day we have means to make changes to core classes of ruby, limited to only that particular domain. Like, to retain the "default" behaviour of ruby core/stdlib classes and refer to these, but anyway, this is for another discussion.) ---------------------------------------- Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicode https://bugs.ruby-lang.org/issues/12306#change-58298 * Author: Sam Saffron * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Time and again there have been rejected feature requests to Ruby core to implement `blank` and `present` protocols across all objects as ActiveSupport does. I am fine with this call and think it is fair. However, for the narrow case of String having `#blank?` and `#present?` makes sense. - Provides a natural extension over `#strip`, `#lstrip` and `#rstrip`. `(" ".strip.length == 0) == " ".blank?` - Plays nicely with ActiveSupport, providing an efficient implementation in Ruby core: see: https://github.com/SamSaffron/fast_blank, implementing blank efficiently requires a c extension. However, if this work is to be done, `#strip` and should probably start dealing with unicode blanks, eg: ``` irb(main):008:0> [0x3000].pack("U") => "���" irb(main):009:0> [0x3000].pack("U").strip.length => 1 ``` So there are 2 questions / feature requests here 1. Can we add blank? and present? to String? 2. Can we amend strip and family to account for unicode per: https://github.com/SamSaffron/fast_blank/blob/master/ext/fast_blank/fast_blank.c#L43-L74 -- https://bugs.ruby-lang.org/ Unsubscribe: