https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112016-04-21T00:20:38ZRuby Issue Tracking SystemRuby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581752016-04-21T00:20:38Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>What about non-Unicode strings?</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581772016-04-21T00:52:21Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I agree with making <code>strip</code> etc. encoding aware, as we did for <code>upcase</code> etc., although priority is low.</p>
<p>I don't see the benefit of <code>blank?</code> and <code>present?</code>, so it has little chance to have them in standard Ruby.<br>
But if <code>String#blank?</code> checks if the string only contains spaces, it means something, but I still don't see real-world use-case.</p>
<p>Matz.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581792016-04-21T02:12:39Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I think that <code>String#blank?</code> equals to <code>/\P{space}/ !~ self</code>.<br>
Is it slow, especially than <code>strip</code>?</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581802016-04-21T02:29:05Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>I think that <code>String#blank?</code> equals to <code>/\P{space}/ !~ self</code>.<br>
Is it slow, especially than <code>strip</code>?</p>
</blockquote>
<p>strip would force allocation of a duplicate string, so is a bit of a non starter.</p>
<p>Yes, in Rails apps this blank very quickly becomes a bottleneck see: <a href="http://tmm1.net/ruby21-profiling/" class="external">http://tmm1.net/ruby21-profiling/</a> for a real world example. I have seen Rails applications spend 2-5% of the time in Active Support <code>String#blank?</code> it is used super aggressively due to the "style" of programming used.</p>
<p>Recently Richard Schneems has been micro optimizing blank? due to this discovery see: <a href="https://github.com/rails/rails/pull/24658" class="external">https://github.com/rails/rails/pull/24658</a></p>
<p>The regex approach will never come close to a native implementation see bench here: <a href="https://github.com/SamSaffron/fast_blank" class="external">https://github.com/SamSaffron/fast_blank</a></p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> as to why this tends to happen so much in Rails world, this is a super common scenario</p>
<ul>
<li>Allow a textinput with User name</li>
<li>Then from controller run</li>
</ul>
<pre><code># one letter name is fine, especially for Chinese names, but blank is not
raise YouGotToAddAName if params[:name].blank?
</code></pre>
<p>In Rails programming it turns out that people are checking for "blankness" of strings in tons and tons of very very common scenario. The speed boost of having a fast native implementation would be very welcome.</p>
<p>I am fine with not adding <code>present?</code> ( unless <code>s.blank?</code>) fills the need just fine</p>
<p>Regarding timing, would a PR adding <code>blank?</code> and unicode support to strip be welcome for 2.4 timeframe?</p>
<p>Regarding non-unicode strings I think strip should just keep working the way it does now.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581812016-04-21T02:50:25Zrafaelfranca (Rafael França)rafael@franca.dev
<ul></ul><p>Other real wold examples:</p>
<p>You want to validate if a username is provided in the form and your user submitted three whitespaces (" "). This is obvious not a valid username so your code checks for it.</p>
<p>In fact every single Rails application that uses:</p>
<pre><code>validates_presense_of :my_attribute
</code></pre>
<p>Uses <code>String#blank?</code>.</p>
<p>Other real example is the mail gem. It also implements <a href="https://github.com/mikel/mail/blob/a6cddd04c3895e0d1101fdc2c566e776ff527e08/lib/mail/utilities.rb#L267-276" class="external"><code>blank?</code></a>. It is used, for example, to check if the <code>from</code> or the <code>to</code> address is filled. An string with only whitespaces is not a valid email address.</p>
<p>This example shows that this is not specific for Rails applications. Any ruby script that will accept user supplied parameters and can't accept strings with only whitespaces will need to have an implementation of <code>blank?</code>.</p>
<p>In my opinion, a shared implementation of <code>blank?</code> in the Ruby standard library would be beneficial to the Ruby community. Right now as it stands, all performance improvements made in <code>blank?</code> for Rails will not be shared by the mail gem or other Ruby programs.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581862016-04-21T03:52:24Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Rafael França wrote:</p>
<blockquote>
<p>You want to validate if a username is provided in the form and your user submitted three whitespaces (" "). This is obvious not a valid username so your code checks for it.</p>
</blockquote>
<p>Is "' || 1=1" a valid username?</p>
<blockquote>
<p>Other real example is the mail gem. It also implements <a href="https://github.com/mikel/mail/blob/a6cddd04c3895e0d1101fdc2c566e776ff527e08/lib/mail/utilities.rb#L267-276" class="external"><code>blank?</code></a>. It is used, for example, to check if the <code>from</code> or the <code>to</code> address is filled. An string with only whitespaces is not a valid email address.</p>
</blockquote>
<p>Validating mail address is a hard job.<br>
"@@@" is not only whitespaces but is it a valid email address?</p>
<p>Your examples don't seem reasonable.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581882016-04-21T04:07:37Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Nobu,</p>
<p>There is a subtle change validation wise with user names:</p>
<p>"Please enter a username, username must not be blank"</p>
<p>vs</p>
<p>"Please enter a valid username, username must not contain the letter E"</p>
<p>Checking for blank is a distinct class of errors (user forgot or entered spaces in one of the fields)</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581892016-04-21T04:09:51Zrafaelfranca (Rafael França)rafael@franca.dev
<ul></ul><blockquote>
<p>Your examples don't seem reasonable.</p>
</blockquote>
<p>Both are real examples, being used in a lot of applications for years, so they are reasonable.</p>
<blockquote>
<p>Validating mail address is a hard job. "@@@" is not only whitespaces but is it a valid email address?</p>
</blockquote>
<p>Indeed, but I was not entering in the merit of email address validation. The validation is a simple check to raise argument error if the email address (or any other field not only email address) is blank. Perhaps I could link to the places were the method is used instead of were it is defined so you can see that it is reasonable:</p>
<p><a href="https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/check_delivery_params.rb#L5" class="external">https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/check_delivery_params.rb#L5</a><br>
<a href="https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/fields/content_disposition_field.rb#L41" class="external">https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/fields/content_disposition_field.rb#L41</a><br>
<a href="https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/fields/mime_version_field.rb#L24" class="external">https://github.com/mikel/mail/blob/df48a05a7fb5a4271e6df12da7afb26a53494a18/lib/mail/fields/mime_version_field.rb#L24</a></p>
<blockquote>
<p>Is "' || 1=1" a valid username?</p>
</blockquote>
<p>No, it is not, but username was only one example. That validation (that is real and is being used in a lot of application) can be used to any attribute, from usernames to comments body. In fact, this same application that we are using to track Ruby's issues is using <code>String#blank?</code>:</p>
<p><a href="https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=validates_presence_of" class="external">https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=validates_presence_of</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581902016-04-21T04:10:53Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Also as a point of reference, have a look at all the calls to String #blank? made from Redmine, the very software powering the bug tracker (present boils down to a call on blank most times)</p>
<p><a href="https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=present" class="external">https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=present</a></p>
<p><a href="https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=blank&type=Code" class="external">https://github.com/redmine/redmine/search?utf8=%E2%9C%93&q=blank&type=Code</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581912016-04-21T04:17:44Zrafaelfranca (Rafael França)rafael@franca.dev
<ul></ul><p>Seems that even Ruby itself would benefits from <code>String#blank?</code></p>
<p><a href="https://github.com/ruby/ruby/search?utf8=%E2%9C%93&q=%22strip.empty%3F%22&type=Code" class="external">https://github.com/ruby/ruby/search?utf8=%E2%9C%93&q=%22strip.empty%3F%22&type=Code</a><br>
<a href="https://github.com/ruby/ruby/search?utf8=%E2%9C%93&q=%22strip.length%22&type=Code" class="external">https://github.com/ruby/ruby/search?utf8=%E2%9C%93&q=%22strip.length%22&type=Code</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581932016-04-21T07:16:28Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Rafael's comment about non-Rails use case of .strip.empty? is very interesting. This shows that adding blank method can benefit wild situations where programmers have no other choice than .strip.empty? which doesn't fluently describe what is wanted.</p>
<p>I know there is blank method in Rails and everybody uses that. However that specific use-case is already treated by ActiveSupport. I see no need to add this in core to save your Rails application, because that solves no problem (apart from being slow). Non-Rails use case seems much more important for people who want this method, especially when they persuade Matz.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581942016-04-21T15:29:26Zschneems (Richard Schneeman)
<ul></ul><p>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. Granted not all of them will only be for <code>present?</code> but some of them will be. In many cases people will want this logic and maybe they cannot or do not wish to pull in Active Support. I find it quite common to either check for presence of a variable and then to see if it is <code>empty?</code> or to <code>strip.empty?</code>.</p>
<p>Here is a quick grep of the examples on my computer (it is by no means comprehensive).</p>
<a name="Nil-and-empty-checks-together"></a>
<h2 >Nil and empty? checks together<a href="#Nil-and-empty-checks-together" class="wiki-anchor">¶</a></h2>
<pre><code>./bundler/lib/bundler/cli/lock.rb: if gems && !gems.empty?
./bundler/lib/bundler/cli/open.rb: editor = [ENV["BUNDLER_EDITOR"], ENV["VISUAL"], ENV["EDITOR"]].find {|e| !e.nil? && !e.empty? }
./bundler/lib/bundler/cli.rb: if ENV["RUBYGEMS_GEMDEPS"] && !ENV["RUBYGEMS_GEMDEPS"].empty?
./carrierwave/lib/carrierwave/uploader/download.rb: return match[1] unless match.nil? || match[1].empty?
./fog/lib/fog/clodo/core.rb: :path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'./fog/lib/fog/clodo/models/compute/server.rb: pubaddrs && !pubaddrs.empty? ? pubaddrs.first['ip'] : nil]
./fog/lib/fog/cloudstack/models/compute/servers.rb: if servers.nil? || servers.empty?
./fog/lib/fog/cloudstack/models/compute/servers.rb: unless servers.nil? || servers.empty?
./git_hub_bub/lib/git_hub_bub/request.rb: self.options[:query] = query if query && !query.empty?
./puma/lib/puma/rack/urlmap.rb: next unless !rest || rest.empty? || rest[0] == ?/
./rack/lib/rack/mock.rb: env[PATH_INFO] = (!uri.path || uri.path.empty?) ? "/" : uri.path
./rack/lib/rack/multipart/parser.rb: elsif !filename && data.empty?
./rack/lib/rack/multipart/parser.rb: if name.nil? || name.empty?
./rack/lib/rack/multipart/parser.rb: if content.nil? || content.empty?
./rack/lib/rack/server.rb: options[:config] = args.last if args.last && !args.last.empty?
./rack/lib/rack/session/abstract/id.rb: value && !value.empty?
./rake/lib/rake/task.rb: add_comment(comment) if comment && ! comment.empty?
./rdoc/lib/rdoc/code_object.rb: if comment and not comment.empty? then
./rdoc/lib/rdoc/parser/c.rb: if no_match and no_match.empty? then
./rest-client/lib/restclient/request.rb: if (!body) || body.empty?
./rpm/lib/new_relic/agent/agent.rb: if data && !data.empty?
./rpm/lib/new_relic/agent/javascript_instrumentor.rb: value.nil? || value.empty?
./rpm/lib/new_relic/agent/obfuscator.rb: if key.nil? || key.empty?
./rubinius/library/rubygems/command.rb: if args.nil? or args.empty? then
./ruby/.ext/common/psych/class_loader.rb: return nil if !klassname || klassname.empty?
./ruby/.ext/common/tk/menu.rb: configure(keys) if keys && !keys.empty?
./ruby/.ext/common/tkextlib/tktable/tktable.rb: obj.configure(keys) if keys && ! keys.empty?
./ruby/.ext/common/tkextlib/tktable/tktable.rb: configure(keys) if keys && ! keys.empty?
./ruby/lib/optparse.rb: elsif !opt or opt.empty?
./ruby/lib/rubygems/command.rb: if args.nil? or args.empty? then
./ruby/lib/rubygems/command.rb: return if option_list.nil? or option_list.empty?
./sass/lib/sass/engine.rb: if content.first && content.first.strip.empty?
./sprockets/lib/sprockets/loader.rb: if cached_asset[:metadata][:included] && !cached_asset[:metadata][:included].empty?
./sprockets/lib/sprockets/loader.rb: if cached_asset[:metadata][:links] && !cached_asset[:metadata][:links].empty?
./sprockets/lib/sprockets/loader.rb: if cached_asset[:metadata][:stubbed] && !cached_asset[:metadata][:stubbed].empty?
./sprockets/lib/sprockets/loader.rb: if cached_asset[:metadata][:required] && !cached_asset[:metadata][:required].empty?
./sprockets/lib/sprockets/loader.rb: if cached_asset[:metadata][:dependencies] && !cached_asset[:metadata][:dependencies].empty?
./www.ruby-lang.org/_plugins/posted_by.rb: if author.nil? || author.empty? || author == 'Unknown Author'
./yard/lib/yard/cli/yri.rb: if @name.nil? || @name.strip.empty?
./yard/lib/yard/parser/ruby/ruby_parser.rb: if comment && !comment.empty?
</code></pre>
<a name="stripempty-checks"></a>
<h2 >strip.empty? checks<a href="#stripempty-checks" class="wiki-anchor">¶</a></h2>
<pre><code>./concurrent-ruby/doc/actor/format.rb: unless chunk.strip.empty? || chunk =~ /\A *#/
./concurrent-ruby/examples/format.rb: unless chunk.strip.empty? || chunk =~ /\A *#/
./fog/lib/fog/rackspace/service.rb: !response.body.strip.empty? &&
./passenger/lib/phusion_passenger/platform_info/compiler.rb: if source.strip.empty?
./passenger/lib/phusion_passenger/platform_info.rb: indent = str.split("\n").select{ |line| !line.strip.empty? }.map{ |line| line.index(/[^\s]/) }.compact.min || 0
./rdoc/lib/rdoc/context.rb: known.value.nil? or known.value.strip.empty?
./rpm/lib/new_relic/cli/commands/deployments.rb: @description = nil if @description && @description.strip.empty?
./rubinius/rakelib/instruction_parser.rb: elsif line.strip.empty?
./rubinius/tools/rubuildius/bin/pastie.rb: return if body.strip.empty?
./rubinius/vm/codegen/field_extract.rb: return '' if out.strip.empty?
./ruby/ext/ripper/tools/strip.rb: if line.strip.empty?
./ruby/ext/tk/extconf.rb: defs.map{|ary| s = ary.join(''); (s.strip.empty?)? "": "-D" << s}
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_XINCLUDES'].strip.empty?) ||
./ruby/ext/tk/extconf.rb: (TkConfig_Info['TK_XLIBSW'] && !TkConfig_Info['TK_XLIBSW'].strip.empty?)
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_XINCLUDES'].strip.empty?
./ruby/ext/tk/extconf.rb: TkConfig_Info['TK_XLIBSW'] && !TkConfig_Info['TK_XLIBSW'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_STUB_LIB_SPEC'].strip.empty? &&
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_BUILD_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_BUILD_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TK_BUILD_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TK_BUILD_LIB_SPEC'].strip.empty?
./ruby/lib/net/http/header.rb: .reject {|str| str.strip.empty? }\
./ruby/lib/rdoc/context.rb: known.value.nil? or known.value.strip.empty?
./sass/lib/sass/engine.rb: if line.strip.empty?
./sass/lib/sass/engine.rb: if value.strip.empty?
./sass/lib/sass/engine.rb: if value.strip.empty? && line.children.empty?
./sass/lib/sass/engine.rb: if content.first && content.first.strip.empty?
./yard/lib/yard/cli/yri.rb: if @name.nil? || @name.strip.empty?
./yard/lib/yard/i18n/text.rb: if line.strip.empty?
./yard/lib/yard/tags/directives.rb: (tag.text && !tag.text.strip.empty?)
./yard/templates/default/docstring/setup.rb: if text.strip.empty? && object.tags(:return).size == 1 && object.tag(:return).text
</code></pre>
<p>In many of these examples the desired result is not clear at a glance. It would simplify code and comprehension to have a common short hand that can be used when it is not appropriate to pull in Active Support.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=581952016-04-21T16:37:38ZPSchambacher (Pierre Schambacher)
<ul></ul><p>Just my 2 cents here but any time I've been writing a pure ruby application, I ended up including active support or copy-pasting the blank? method.</p>
<p>There's a lot of applications of pure ruby code to know if the string that we have is just blank space: parsing a CSV, reading the response from a web request, receive something from a socket, reading a file, ... empty? is not always enough since there's scenarii where having a couple blank spaces make no more sense than nothing at all.</p>
<p>I also see the point from Nobu about using regular expressions to validate the input but that's a sword with 2 edges. In theory it's great but in practice having the perfect regular expression is pretty difficult (see <a href="http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/" class="external">http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/</a> for instance). In lots of cases, having "something that's not blank" is just the simplest and surest thing to do.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=582922016-04-24T15:14:45Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Richard Schneeman wrote:</p>
<blockquote>
<p>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. Granted not all of them will only be for <code>present?</code> but some of them will be. In many cases people will want this logic and maybe they cannot or do not wish to pull in Active Support. I find it quite common to either check for presence of a variable and then to see if it is <code>empty?</code> or to <code>strip.empty?</code>.</p>
<p>Here is a quick grep of the examples on my computer (it is by no means comprehensive).</p>
<a name="Nil-and-empty-checks-together"></a>
<h2 >Nil and empty? checks together<a href="#Nil-and-empty-checks-together" class="wiki-anchor">¶</a></h2>
</blockquote>
<p>you can already write <code>str&.empty?</code>.</p>
<blockquote>
<a name="stripempty-checks"></a>
<h2 >strip.empty? checks<a href="#stripempty-checks" class="wiki-anchor">¶</a></h2>
<pre><code>./concurrent-ruby/doc/actor/format.rb: unless chunk.strip.empty? || chunk =~ /\A *#/
./concurrent-ruby/examples/format.rb: unless chunk.strip.empty? || chunk =~ /\A *#/
./fog/lib/fog/rackspace/service.rb: !response.body.strip.empty? &&
./passenger/lib/phusion_passenger/platform_info/compiler.rb: if source.strip.empty?
./passenger/lib/phusion_passenger/platform_info.rb: indent = str.split("\n").select{ |line| !line.strip.empty? }.map{ |line| line.index(/[^\s]/) }.compact.min || 0
./rdoc/lib/rdoc/context.rb: known.value.nil? or known.value.strip.empty?
./rpm/lib/new_relic/cli/commands/deployments.rb: @description = nil if @description && @description.strip.empty?
./rubinius/rakelib/instruction_parser.rb: elsif line.strip.empty?
./rubinius/tools/rubuildius/bin/pastie.rb: return if body.strip.empty?
./rubinius/vm/codegen/field_extract.rb: return '' if out.strip.empty?
./ruby/ext/ripper/tools/strip.rb: if line.strip.empty?
./ruby/ext/tk/extconf.rb: defs.map{|ary| s = ary.join(''); (s.strip.empty?)? "": "-D" << s}
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_XINCLUDES'].strip.empty?) ||
./ruby/ext/tk/extconf.rb: (TkConfig_Info['TK_XLIBSW'] && !TkConfig_Info['TK_XLIBSW'].strip.empty?)
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_XINCLUDES'].strip.empty?
./ruby/ext/tk/extconf.rb: TkConfig_Info['TK_XLIBSW'] && !TkConfig_Info['TK_XLIBSW'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_STUB_LIB_SPEC'].strip.empty? &&
./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_BUILD_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TCL_BUILD_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TK_BUILD_STUB_LIB_SPEC'].strip.empty?
./ruby/ext/tk/extconf.rb: !TclConfig_Info['TK_BUILD_LIB_SPEC'].strip.empty?
./ruby/lib/net/http/header.rb: .reject {|str| str.strip.empty? }\
./ruby/lib/rdoc/context.rb: known.value.nil? or known.value.strip.empty?
./sass/lib/sass/engine.rb: if line.strip.empty?
./sass/lib/sass/engine.rb: if value.strip.empty?
./sass/lib/sass/engine.rb: if value.strip.empty? && line.children.empty?
./sass/lib/sass/engine.rb: if content.first && content.first.strip.empty?
./yard/lib/yard/cli/yri.rb: if @name.nil? || @name.strip.empty?
./yard/lib/yard/i18n/text.rb: if line.strip.empty?
./yard/lib/yard/tags/directives.rb: (tag.text && !tag.text.strip.empty?)
./yard/templates/default/docstring/setup.rb: if text.strip.empty? && object.tags(:return).size == 1 && object.tag(:return).text
</code></pre>
</blockquote>
<p>Most of them looks different from ActiveSupport's <code>String#blank?</code> which is Unicode aware.<br>
If you replace them with <code>String#blank</code>, it will break those code.<br>
It is what we wonder about.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=582982016-04-24T19:01:25Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Just one comment, not related to the suggestion itself but to one<br>
other comment made above.</p>
<p>Richard Schneeman wrote:</p>
<blockquote>
<p>I think this is useful outside of Rails. The Active Support module has<br>
87 million downloads on rubygems.org while Railties has only 53 million<br>
downloads. So 34 million times people have needed to use Active Support<br>
without Railties, this is a huge number.</p>
</blockquote>
<p>This is not a good comparison to show the "most downloads" statistics alone.</p>
<p>Many downloads are bundled together with prior dependencies, e. g. rails.<br>
All the Active* gems are, in most cases, tied to the usage of rails. Most<br>
people who will do a "gem install rails" won't know what the individual<br>
active* gems do or how they work.</p>
<p>Not everyone who uses ruby, also uses rails. And idioms that are available<br>
to ruby itself, should primarily make and see fit within ruby itself,<br>
not to external gems/projects.</p>
<p>A lot of rails has also influenced ruby itself too, but rails is not ruby.</p>
<p>String#blank? may fit well into the active* world but for a regular<br>
string object, it is not entirely clear what it should mean. Should<br>
it mean whether the String contains one or more "blanks"? In this case,<br>
it would have to check for at least one ' ' in that string object.</p>
<p>But this is not what the method does. The documentation is:</p>
<p>"A string is blank if it's empty or contains whitespaces only"</p>
<p>So the name of the method is actually wrong in my opinion, since it also<br>
checks whether the string is empty. Is an empty string a blank string<br>
or does it include a "blank" string or character? In an empty string,<br>
there surely is no space character.</p>
<p>It sure enough does not include any ' ' so why would it be considered<br>
blank?</p>
<p>And for string objects such as:</p>
<p>"This cat is a happy cat.".blank?</p>
<p>This would return false in the above definition, but it sure enough<br>
does include the ' ' character.</p>
<p>I assume the method .blank? here more does a .strip.empty? check<br>
combined. Calling this operation ".blank?" still does not seem to<br>
be right IMO.</p>
<p>An advantage, and a huge point in favour of ninja patching (also called<br>
monkey patching), you model ruby within the particular domain thinking.<br>
(And with refinements, perhaps one day we have means to make changes to<br>
core classes of ruby, limited to only that particular domain. Like, to<br>
retain the "default" behaviour of ruby core/stdlib classes and refer to<br>
these, but anyway, this is for another discussion.)</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=582992016-04-24T23:03:33Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Nobu,</p>
<p>Regarding:</p>
<blockquote>
<p>Most of them looks different from ActiveSupport's String#blank? which is Unicode aware.</p>
</blockquote>
<p>I think this change (if it happens) must come with unicode aware strip/rstrip/lstrip (and underlying is_space) for UTF-8 encoded strings. It is clear that there is a desire to make string operations encoding aware so this would have to be lockstep. All of the <code>strip.length==0</code> in core can safely be changed to a unicode aware <code>blank?</code> function with zero regression.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583042016-04-25T02:37:53Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Sam Saffron wrote:</p>
<blockquote>
<p>All of the <code>strip.length==0</code> in core can safely be changed to a unicode aware <code>blank?</code> function with zero regression.</p>
</blockquote>
<p>Can you elaborate with this? For instance this example</p>
<pre><code>./ruby/ext/tk/extconf.rb: !TkConfig_Info['TK_XINCLUDES'].strip.empty?
</code></pre>
<p>is clear that the XINCLUDES indicates this string is passed to a shell script. The call of strip here surely intends stripping shell's definition of whitespaces, not the Unicode one. Do you think Unicode-aware strip cannot introduce regressions for this kind of situations?</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583062016-04-25T10:05:59Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>Several comments, all in one post:</p>
<ol>
<li>
<p>.blank? definitely cannot check for all problems in an input field, but it seems to be used very often because it very easily catches a frequent user mistake. More precise checking is more difficult, more application-dependent, and will catch less mistakes. So I'm not surprised that may programmers use it, even if it's just only a first step.</p>
</li>
<li>
<p>If we want to make Ruby 3 times faster by Ruby 3.0 (see Matz's keynote at last year's Ruby Kaigi), then ignoring a request to speed up functionality that may take up to 2% to 5% of time in the most widely used application of Ruby seems to be rather counter-purpose.</p>
</li>
<li>
<p>Regarding Unicode awareness, there are many ways to extend the definition of white space. See e.g. <a href="https://discourse.wicg.io/t/whitespace-is-hard-and-buggy-can-we-normalize-it/1436" class="external">https://discourse.wicg.io/t/whitespace-is-hard-and-buggy-can-we-normalize-it/1436</a>. The experience of Rails may be very valuable, but we'll have to look at it in detail.</p>
</li>
<li>
<p>Regarding backwards compatibility, for upcase/downcase/..., Matz has said that he's willing to make it backwards-incompatible in edge cases (if you have non-ASCII data, but <em>really</em> only want ASCII to change case). That might apply here, too. But we have to discuss it.</p>
</li>
</ol> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583072016-04-25T11:32:19Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>Hello Sam,</p>
<p>Just in private for the moment. I'm trying to find the definition of<br>
blank? in the rails source code. I expected it somewhere around<br>
here:<a href="https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/string" class="external">https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/string</a><br>
but I didn't find anything.</p>
<p>Regards, Martin.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583082016-04-25T11:38:48Zjbilbo (Jonathan Hernandez)jbilbo@gmail.com
<ul></ul><p><a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb" class="external">https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb</a></p>
<p>Martin Dürst wrote:</p>
<blockquote>
<p>Hello Sam,</p>
<p>Just in private for the moment. I'm trying to find the definition of<br>
blank? in the rails source code. I expected it somewhere around<br>
here:<a href="https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/string" class="external">https://github.com/rails/rails/tree/master/activesupport/lib/active_support/core_ext/string</a><br>
but I didn't find anything.</p>
<p>Regards, Martin.</p>
</blockquote> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583092016-04-25T14:12:00Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Yui NARUSE wrote:</p>
<blockquote>
<p>Richard Schneeman wrote:</p>
<blockquote>
<a name="Nil-and-empty-checks-together"></a>
<h2 >Nil and empty? checks together<a href="#Nil-and-empty-checks-together" class="wiki-anchor">¶</a></h2>
</blockquote>
<p>you can already write <code>str&.empty?</code>.</p>
</blockquote>
<p>It's not useful. The logic we are looking for is basically !(nil? || empty?). str&.empty? would return nil for nil and true for '' (empty).</p>
<p>If Ruby provided "non_empty?" we could use it like str&.non_empty? and it would make sense.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583142016-04-25T18:57:00Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>Yui NARUSE wrote:</p>
<blockquote>
<p>Richard Schneeman wrote:</p>
<blockquote>
<a name="Nil-and-empty-checks-together"></a>
<h2 >Nil and empty? checks together<a href="#Nil-and-empty-checks-together" class="wiki-anchor">¶</a></h2>
</blockquote>
<p>you can already write <code>str&.empty?</code>.</p>
</blockquote>
<p>It's not useful. The logic we are looking for is basically !(nil? || empty?). str&.empty? would return nil for nil and true for '' (empty).</p>
<p>If Ruby provided "non_empty?" we could use it like str&.non_empty? and it would make sense.</p>
</blockquote>
<p>Ah, yes true.<br>
I just remind old my proposal <a href="https://bugs.ruby-lang.org/issues/12075" class="external">https://bugs.ruby-lang.org/issues/12075</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583322016-04-26T10:12:30Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Shyouhei Urabe wrote:</p>
<blockquote>
<p>is clear that the XINCLUDES indicates this string is passed to a shell script. The call of strip here surely intends stripping shell's definition of whitespaces, not the Unicode one. Do you think Unicode-aware strip cannot introduce regressions for this kind of situations?</p>
</blockquote>
<p>In this particular case I fail to see how this can cause any problem.</p>
<pre><code>sam@ubuntu ruby % ruby -e 'p ARGV[0]' test
" test "
</code></pre>
<p>Most shells these days allow you to pass in UTF-8 strings, but saying that there is one real case in the universe where someone really wanted to include the library <code> hacky </code> as opposed to the lib called <code>hacky</code> when building tk is not true. blank? and utf8 aware strip would work fine here and not break a single real example in this usage.</p>
<p>That said, internal uses of strip while dealing with shell params passed to ARGV may need some care, but requiring people start quoting leading and trailing unicode spaces is not really a major breaking change imo.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583412016-04-27T01:40:07Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Sam Saffron wrote:</p>
<blockquote>
<p>In this particular case I fail to see how this can cause any problem.</p>
</blockquote>
<p>You failed to see the problem because you could not imagine a file path containing U+3000. That is not very rare in cultures with ideographics. No, I'm not against categorizing such path being insane. But they ARE there.</p>
<blockquote>
<p>Most shells these days allow you to pass in UTF-8 strings, but saying that there is one real case in the universe where someone really wanted to include the library <code> hacky </code> as opposed to the lib called <code>hacky</code> when building tk is not true. blank? and utf8 aware strip would work fine here and not break a single real example in this usage.</p>
</blockquote>
<p>U+3000 inside of a path should not be stripped. That should break existing working codes.</p>
<blockquote>
<p>That said, internal uses of strip while dealing with shell params passed to ARGV may need some care, but requiring people start quoting leading and trailing unicode spaces is not really a major breaking change imo.</p>
</blockquote>
<p>So you admit this change do emit regressions, not zero. You just say this is minor.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583482016-04-27T06:51:50Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>I just come back tmm1's bloc: <a href="http://tmm1.net/ruby21-profiling/" class="external">http://tmm1.net/ruby21-profiling/</a><br>
You guys are talking on this.</p>
<p>But I'm wondering whether it is still true after <a href="https://github.com/rails/rails/pull/24658" class="external">https://github.com/rails/rails/pull/24658</a> is merged.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583492016-04-27T06:57:53Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Shyouhei Urabe wrote:</p>
<blockquote>
<p>You failed to see the problem because you could not imagine a file path containing U+3000. That is not very rare in cultures with ideographics. No, I'm not against categorizing such path being insane. But they ARE there.</p>
</blockquote>
<p>I can see that ... but I can not see someone trying to build tk and expecting that including <code> </code> would work.</p>
<p>Nobu,</p>
<p>Yes the micro optimisations in blank only yield a 10-30% improvement see the benches in <a href="https://github.com/SamSaffron/fast_blank" class="external">https://github.com/SamSaffron/fast_blank</a> native is_space loop is often 10x faster. feel free to rerun the benches against latest code.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583532016-04-27T07:59:29Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>note this method exists in .NET String</p>
<p><a href="https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx" class="external">https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx</a></p>
<p>and in Python</p>
<p><a href="http://www.tutorialspoint.com/python/string_isspace.htm" class="external">http://www.tutorialspoint.com/python/string_isspace.htm</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583612016-04-28T00:08:52Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Just to expand on how hard this is to get right without the framework providing it</p>
<p>See:</p>
<p><a href="https://gist.github.com/SamSaffron/d1a9cc8e141e7415e06306369fdedfe5" class="external">https://gist.github.com/SamSaffron/d1a9cc8e141e7415e06306369fdedfe5</a></p>
<p><code>/[[:^space:]]/ === str</code> can cause significantly more data to allocate including invisible MatchData and Strings vs</p>
<p><code>/\A[[:space:]]*\z/ === str</code></p>
<p>(depending on the string being tested)</p>
<p>There is no way to invoke the regex engine without it magically setting a pile of globals, making it very inefficient to do lots of things with regex. 3 years ago when I brought this up, Nobu suggested allowing String#include? to accept a regex and set no globals, that may be a way to get a bunch of perf out of the regex engine. Or simply stop with all the globals in Ruby 3 and have specific methods for getting match data always used. I don't know.</p>
<p>My point is, doing something even trivial here (test if string is blank) is practically impossible to do fast in Ruby today.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583692016-04-28T07:42:20Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Sam Saffron wrote:</p>
<blockquote>
<p>There is no way to invoke the regex engine without it magically setting a pile of globals, making it very inefficient to do lots of things with regex. 3 years ago when I brought this up, Nobu suggested allowing String#include? to accept a regex and set no globals, that may be a way to get a bunch of perf out of the regex engine. Or simply stop with all the globals in Ruby 3 and have specific methods for getting match data always used. I don't know.</p>
</blockquote>
<p>I'm sorry that I don't remember it.<br>
Can't you show the pointer?</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583702016-04-28T08:06:15Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>Sure Nobu,</p>
<p><a href="https://bugs.ruby-lang.org/issues/8206" class="external">https://bugs.ruby-lang.org/issues/8206</a></p>
<p>very close to this feature request that I created years ago.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=583732016-04-28T14:07:38Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/8206">Feature #8206</a>: Should Ruby core implement String#blank? </i> added</li></ul> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=585092016-05-06T05:58:04Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Anyways, given the real-world use-case, I now think it's a good idea to have something that does strip.empty? -equivalent method for String. Maybe shell-related use cases can be migrated to shellwords standard library, like by creating Shellwords.shellstrip method or something.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=586862016-05-17T08:44:30Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/8110">Feature #8110</a>: Regex methods not changing global variables</i> added</li></ul> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=587492016-05-19T18:55:53Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Experimentally implemented an String#blank? with Intel STTNI (SSE 4.2; Nehalem or later)<br>
<a href="https://github.com/ruby/ruby/commit/e6bc209abf81d53c2e3374dc52c2a128570c6055" class="external">https://github.com/ruby/ruby/commit/e6bc209abf81d53c2e3374dc52c2a128570c6055</a></p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=587562016-05-20T00:23:10Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>The instruction doesn't seem worth specializing, at least.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=587672016-05-20T09:25:15Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/12403">Feature #12403</a>: Optimise Regexp#match?</i> added</li></ul> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=695962018-01-16T15:03:14Zana06 (Ana Maria Martinez Gomez)
<ul></ul><p>I would also like to have <code>blank?</code> and <code>present?</code> in Ruby. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> says he doesn't see the benefit of having them, but I think they improve readability.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=711662018-03-22T21:03:11Zbusterb (Brent Cook)busterb@gmail.com
<ul></ul><p>This seems to be the number one time-wasters in my project (metasploit-framework), where nobody can agree if we should use it, shouldn't, whether it affects performance, behavior on nil, etc.</p>
<p>It would be great if this just got added to the standard library for nil and string. It's practically part of the Ruby standard anyway.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=723602018-06-04T03:03:17Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/13">@matz (Yukihiro Matsumoto)</a> ... is there any way we can revise this and act on it? I really want to kill off my "fast_blank" gem. The 2 decisions that need to be made are:</p>
<ol>
<li>Will MRI accept String#blank? which returns exactly the same as String#strip.empty?</li>
<li>Will MRI accept changing #strip to be unicode space aware so it is consistent with Regexp?</li>
</ol>
<p>#match? helps but it is not as fast as a native implementation.</p>
<pre><code>sam@ubuntu rails % irb
irb(main):001:0> str = "\u202f"
=> " "
irb(main):002:0> str = "\u202f".strip.length
=> 1
irb(main):005:0> str = "\u202f".match?(/\A[[:space:]]*\z/)
=> true
</code></pre>
<p>The real world use case of this is slowly moving us to Ruby 3x3, by adding #blank? we can have an optimised implementation of #strip#empty? this is used widely in MRI codebase and other projects (non Rails including)</p>
<p>An alternative may be erasing #strip#empty? in insns or something but I wonder if this is both risky and simply adding #blank? is easier.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=723702018-06-04T07:50:53Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>As <code>String#upcase</code> and family are Unicode case aware now, so the second feels somewhat reasonable.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=729482018-07-13T15:43:35Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Sam suggested the topic to be discussed at the next upcoming ruby<br>
developer meeting here recently:</p>
<p><a href="https://bugs.ruby-lang.org/issues/14861" class="external">https://bugs.ruby-lang.org/issues/14861</a></p>
<p>I'll add a few comments to the issue here as a consequence.</p>
<p>I am mostly neutral to the whole issue at hand, though up to slightly in<br>
disfavour of adding #blank? and respectively #present?.</p>
<p>It's not that I do not understand the use case; I simply don't think<br>
the method names make a lot of sense. What is a "blank" string really?</p>
<p>If it is an empty string, then it should be called an empty string. But<br>
rails also considers an empty array to be "blank", so I am confused -<br>
see lateron in this comment.</p>
<p>I understand that we want to query the whitespace status via #blank? but<br>
then why not call it #whitespace? or something similar? And why does<br>
ActiveSupport use #blank? across several different classes with a<br>
different meaning/behaviour - such as on arrays?</p>
<p>So, my problem is primarily with the name, only secondarily the<br>
functionality in itself.</p>
<p>Note also that while active* is big, active* is not ruby as a whole.<br>
For core functionality, the reasoning should be "because lots of ruby<br>
people may need this or that" rather than "because it is used in<br>
active*, we should make it part of ruby", in my opinion. These two<br>
can often be synonymous but the reasoning should still come from<br>
a general user's perspective.</p>
<p>As for #present?, I have even less of an idea what this is supposed<br>
to do. I had a look at the docu for ActiveSupport:</p>
<p><a href="http://guides.rubyonrails.org/active_support_core_extensions.html#blank-questionmark-and-present-questionmark" class="external">http://guides.rubyonrails.org/active_support_core_extensions.html#blank-questionmark-and-present-questionmark</a></p>
<p>"The method present? is equivalent to !blank?."</p>
<p>I think that is also weird from a design point of view.</p>
<p>It's a bit like .select versus .reject, except just for a (forward<br>
or backwards) query. Would you have guessed that "#present?" is the<br>
opposite to "#blank?"? I would not have.</p>
<p>"The following values are considered to be blank in a Rails application"<br>
" empty arrays and hashes, and"<br>
" any other object that responds to empty? and is empty."</p>
<p>Is also a weird design choice. In the latter blank? is suddenly<br>
equivalent to .empty? for general objects - but we already have<br>
that method name for e. g. Strings. So I don't think this is a<br>
good design decision that Rails did, but that is my opinion and<br>
I am sure many rails people think it's a great design decision.</p>
<p>It's good that ruby is that flexible though.</p>
<p>As for #strip handling unicode, if it is possible I think it would<br>
be nice. People could then assume that calling .strip on a unicode<br>
string would remove e. g. newlines and whitespace on the ends/sides.</p>
<p>I don't know how difficult it would be to have this; guess Martin<br>
Dürst knows more here. Do all languages support this behaviour<br>
by the way? Perhaps some languages have no whitespace, like<br>
some symbols-based languages perhaps.</p>
<p>I should however had also add that I am not really that against the<br>
proposal. It's no real problem at all for me if it is added, so I<br>
am mostly neutral, only with a slight inching towards against the<br>
first part of the proposal. (The unicode extension is I think<br>
perfectly fine; I think it may have been better to split this<br>
issue into two separate ones though.)</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=729872018-07-18T06:24:54Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>If the requirement is adding a predicate method to check if a string contains only characters with space property (defined by Unicode), it's OK to add the method. I am not fully satisfied with the name <code>blank?</code> because it can cause confusion whether it checks a character in a string or all characters in a string. <code>blank?</code> is still acceptable though it's not the best.</p>
<p>Regarding <code>present?</code>, I am not going to agree.</p>
<p>Matz.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=730352018-07-20T10:53:54Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I've proposed <code>String#space_only?</code>, like <code>String#ascii_only?</code>.</p> Ruby master - Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicodehttps://bugs.ruby-lang.org/issues/12306?journal_id=730712018-07-22T22:55:19Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><blockquote>
<p>I've proposed String#space_only?, like String#ascii_only?.</p>
</blockquote>
<p>I very much support <code>#space_only?</code>, it still fills the original goal of the ticket which is to eliminate the the fast_blank gem!</p>