Issue #12306 has been updated by Yui NARUSE.


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. Granted not all of them will only be for `present?` 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 `empty?` or to `strip.empty?`.
> 
> Here is a quick grep of the examples on my computer (it is by no means comprehensive).
> 
> ## Nil and empty? checks together

you can already write `str&.empty?`.

> ## strip.empty? checks
> 
> ./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

Most of them looks different from ActiveSupport's String#blank? which is Unicode aware.
If you replace them with String#blank, it will break those code.
It is what we wonder about.

----------------------------------------
Feature #12306: Implement String #blank? #present? and improve #strip and family to handle unicode
https://bugs.ruby-lang.org/issues/12306#change-58292

* 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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>