Issue #12306 has been updated by Rafael Frana.


Other real wold examples:

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.

In fact every single Rails application that uses:

```
validates_presense_of :my_attribute
```

Uses `String#blank?`.

Other real example is the mail gem. It also implements [`blank?`](https://github.com/mikel/mail/blob/a6cddd04c3895e0d1101fdc2c566e776ff527e08/lib/mail/utilities.rb#L267-276). It is used, for example, to check if the `from` or the `to` address is filled. An string with only whitespaces is not a valid email address.

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 `blank?`.

In my opinion, a shared implementation of `blank?` in the Ruby standard library would be beneficial to the Ruby community. Right now as it stands, all performance improvements made in `blank?` for Rails will not be shared by the mail gem or other Ruby programs.

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

* 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>