aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object/blank.rb
Commit message (Collapse)AuthorAgeFilesLines
* [Active Support] require_relative => requireAkira Matsuda2017-10-211-1/+1
| | | | This basically reverts 8da30ad6be34339124ba4cb4e36aea260dda12bc
* [Active Support] `rubocop -a --only Layout/EmptyLineAfterMagicComment`Koichi ITO2017-07-111-0/+1
|
* Use frozen-string-literal in ActiveSupportKir Shatrov2017-07-091-0/+1
|
* [Active Support] require => require_relativeAkira Matsuda2017-07-011-1/+1
|
* applies new string literal convention in activesupport/libXavier Noria2016-08-061-1/+1
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* adds require for Regexp#match?Xavier Noria2016-07-221-0/+2
|
* performance boost for String#blank? in Ruby 2.4Xavier Noria2016-07-221-1/+1
| | | | | | Some casual benchmarks showed a 2x factor. All credit goes to @nurse.
* restores the regexp used in String#blank?Xavier Noria2016-04-291-4/+3
| | | | | | | | | | This commit undoes 54243fe. Reason: Further investigation has shown the benefit is not so clear generally speaking. There is a long discussion and several benchmarks in the PR #24658 if you are interested in the details.
* rewords code comment [ci skip]Xavier Noria2016-04-241-3/+3
| | | | | | This alternative flows better. [Richard Schneeman & Xavier Noria]
* just say nothing about why this regexp is slower [ci skip]Xavier Noria2016-04-211-1/+1
| | | | | | | | | Further investigation seems to disprove that backtracking is the reason why the positive variant is slower, see https://github.com/rails/rails/pull/24658#issuecomment-213079710 so, just say nothing about it, only assert it is slower.
* restores code comments in String#blank? [ci skip]Xavier Noria2016-04-211-2/+6
| | | | | | | | | | | | When you come here without context, it is important to hightlight that checking the predicate is worthwhile due to the observation that blank strings are often empty. So you complicate the code (which has a cost in terms of readability and aesthetics), but statistically makes sense. Then, you also need to explain why the second operand is so convoluted. Otherwise, you wonder why this line is written precisely this way. That is what code comments are for.
* Remove unused `BLANK_RE`Ryuta Kamizono2016-04-211-2/+0
| | | | Follow up to #24658.
* Speed up String#blank? Regexschneems2016-04-201-6/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow up on https://github.com/rails/rails/commit/697384df36a939e565b7c08725017d49dc83fe40#commitcomment-17184696. The regex to detect a blank string `/\A[[:space:]]*\z/` will loop through every character in the string to ensure that all of them are a `:space:` type. We can invert this logic and instead look for any non-`:space:` characters. When that happens, we would return on the first character found and the regex engine does not need to keep looking. Thanks @nellshamrell for the regex talk at LSRC. By defining a "blank" string as any string that does not have a non-whitespace character (yes, double negative) we can get a substantial speed bump. Also an inline regex is (barely) faster than a regex in a constant, since it skips the constant lookup. A regex literal is frozen by default. ```ruby require 'benchmark/ips' def string_generate str = " abcdefghijklmnopqrstuvwxyz\t".freeze str[rand(0..(str.length - 1))] * rand(0..23) end strings = 100.times.map { string_generate } ALL_WHITESPACE_STAR = /\A[[:space:]]*\z/ Benchmark.ips do |x| x.report('current regex ') { strings.each {|str| str.empty? || ALL_WHITESPACE_STAR === str } } x.report('+ instead of * ') { strings.each {|str| str.empty? || /\A[[:space:]]+\z/ === str } } x.report('not a non-whitespace char') { strings.each {|str| str.empty? || !(/[[:^space:]]/ === str) } } x.compare! end # Warming up -------------------------------------- # current regex # 1.744k i/100ms # not a non-whitespace char # 2.264k i/100ms # Calculating ------------------------------------- # current regex # 18.078k (± 8.9%) i/s - 90.688k # not a non-whitespace char # 23.580k (± 7.1%) i/s - 117.728k # Comparison: # not a non-whitespace char: 23580.3 i/s # current regex : 18078.2 i/s - 1.30x slower ``` This makes the method roughly 30% faster `(23.580 - 18.078)/18.078 * 100`. cc/ @fxn
* ~3.5x speedup of String#blank? for empty stringsXavier Noria2016-04-201-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | See the rationale in the comment in this patch. To benchmark this I ran a number of variations, ultimately narrowing to require 'benchmark/ips' str = '' regexp = /\A[[:space:]]*\z/ Benchmark.ips do |x| x.report('regexp') { regexp === str } x.report('empty') { str.empty? || regexp === str } x.compare! end This benchmark has consistently reported speedups around 3.5x: Calculating ------------------------------------- regexp 69.197k i/100ms empty 115.468k i/100ms ------------------------------------------------- regexp 2. 6.3%) i/s - 13.839M empty 9. 8.8%) i/s - 47.804M Comparison: empty: 9642607.6 i/s regexp: 2768351.9 i/s - 3.48x slower Sometimes even reaching 4x. Running the same bechmark on strings of 10 or 100 characters (with whitespace or present) has shown a slowdown of just about 1.01/1.02. Marginal, we seem to have a worthwhile trade-off here.
* Short-circuit `blank?` on date and time valuesAndrew White2015-09-211-0/+11
| | | | | | | The concept of a blank date or time doesn't make sense so we can short circuit the calls for `blank?` on these classes to gain small speed boost. Fixes #21657
* File encoding is defaulted to utf-8 in Ruby >= 2.1Akira Matsuda2015-09-181-2/+0
|
* docs, make `blank?` behavior clear. Closes #21468. [ci skip]Yves Senn2015-09-021-2/+2
|
* We tenderized the wrong method! Object#try already had the yield option, ↵David Heinemeier Hansson2014-08-291-14/+2
| | | | just needed some tenderloving instance_eval to fit the bill
* Use instance_eval on @tenderlove's suggestion :trollface:David Heinemeier Hansson2014-08-291-4/+5
|
* Update examples to show real worthDavid Heinemeier Hansson2014-08-291-1/+1
|
* Added yield to Object#presenceDavid Heinemeier Hansson2014-08-291-1/+12
|
* blank? and present? commit to return singletons [Xavier Noria & Pavel Pravosud]Xavier Noria2013-12-281-18/+42
| | | | | | | | | | | | | | | | | | The contract of blank? and present? was in principle to return Object, as we generally do, the test suite and description was consistent with that, but some examples had comments like "# => true". This cannot be unclear, we either fix the examples, or update the contract. Since users may be already assuming singletons due to the examples and the fact that they were returned before 30ba7ee, the safest option seems to be to revise the contract and the implementation of String#blank? The motivation for 30ba7ee was to improve the performance of the predicate, the refactor based on === is on par regarding speed. With this commit we start documenting return types using YARD conventions. We plan to document return types gradually.
* Extracted blank matcher regexp to constantPaul Nikitochkin2013-12-281-1/+3
|
* Expand double-negative in String#blank? regexAman Gupta2013-11-201-1/+1
|
* update some AS code examples to 1.9 hash syntax [ci skip]Francesco Rodriguez2012-09-121-8/+1
|
* String quotes and trailing spacesAlexey Gaziev2012-04-291-5/+5
|
* remove unnecessary require core_ext/string/encodingSergey Nartimov2012-02-141-1/+0
|
* remove support for ruby 1.8 in AS String extensionsSergey Nartimov2011-12-211-9/+1
|
* missing require of string/encoding to have access to encoding_aware?Damien Mathieu2011-07-201-0/+1
|
* fix String#blank? on binary strings.Damien Mathieu2011-07-201-1/+6
|
* need magic commentsAkira Matsuda2011-07-141-0/+2
|
* treat fullwidth whitespace as a blank characterAkira Matsuda2011-07-141-1/+5
|
* Remove extra white spaces on ActiveSupport docs.Sebastian Martinez2011-05-231-1/+1
|
* revises comment for blank? methodVijay Dev2011-05-071-3/+3
|
* Revert "Small edit to the if statement simplification for blank? on the ↵Xavier Noria2011-05-061-1/+1
| | | | | | | | | | | | | | Object class. An Object doesn't need to be nil and empty to be blank? just one or the other." Reason: the original code is correct, see !nil && !empty -> !(nil || empty) -> !blank That said, this example has too many negations for my linking. Think should be revised to be more positive and straightforward to read. This reverts commit a4b6e3d9be338e93320606f7cb14759b26ced8df.
* Small edit to the if statement simplification for blank? on the Object ↵Caley Woods2011-05-061-1/+1
| | | | class. An Object doesn't need to be nil and empty to be blank? just one or the other.
* commit copy-edit: simplifies blank? rdoc and revises formattingXavier Noria2011-04-191-24/+23
|
* Revert "Formated docs"Xavier Noria2011-04-191-9/+9
| | | | | | | Reason: "To be blank" and "to be empty" belongs to our everyday terminology, they go in regular font. This reverts commit 280a8709923d2bf707ec4d82ac1e5cb14941da3d.
* Formated docsSebastian Martinez2011-04-181-9/+9
|
* Formatting docsSebastian Martinez2011-04-171-10/+10
|
* Formatting examplesSebastian Martinez2011-04-171-6/+12
|
* Documented String#blank?Sebastian Martinez2011-04-171-1/+7
|
* Documented Hash#blank?Sebastian Martinez2011-04-171-1/+6
|
* Documented Array#blank?Sebastian Martinez2011-04-171-1/+6
|
* Documented TrueClass#blank?Sebastian Martinez2011-04-171-1/+5
|
* Documented FalseClass#blank?Sebastian Martinez2011-04-171-1/+5
|
* Documented NilClass#blank?Sebastian Martinez2011-04-171-1/+5
|
* copy-edits 108561fXavier Noria2011-03-051-1/+1
|
* Adds link to Object.blank? from Object.present?Jonathon D. Jones2011-03-051-1/+1
|
* Deletes trailing whitespaces (over text files only find * -type f -exec sed ↵Santiago Pastorino2010-08-141-1/+1
| | | | 's/[ \t]*$//' -i {} \;)