Skip to content

Commit 2dd3164

Browse files
committed
Add Symbol#blank? to skip respond_to?(:empty?)
Any classes that don't implement #blank? will fall back to Object's implementation, which checks whether #empty? is defined before either using #empty? or implicit truthiness. Since checking whether #empty? is defined is expensive, some core classes (Array/Hash) alias #blank? directly to #empty? to skip the respond_to? check. This commit applies the alias optimization to Symbol. #blank? is called on Symbols for many Active Record query methods (select, includes, group, order, joins, etc.) so it seems reasonable to define the #blank? alias on Symbol as well.
1 parent 4c5c904 commit 2dd3164

File tree

1 file changed

+8
-0
lines changed
  • activesupport/lib/active_support/core_ext/object

1 file changed

+8
-0
lines changed

activesupport/lib/active_support/core_ext/object/blank.rb

+8
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ class Hash
100100
alias_method :blank?, :empty?
101101
end
102102

103+
class Symbol
104+
# A Symbol is blank if it's empty:
105+
#
106+
# :''.blank? # => true
107+
# :symbol.blank? # => false
108+
alias_method :blank?, :empty?
109+
end
110+
103111
class String
104112
BLANK_RE = /\A[[:space:]]*\z/
105113
ENCODED_BLANKS = Concurrent::Map.new do |h, enc|

0 commit comments

Comments
 (0)