Reputation: 1701
I would like to know how Rails handles the method blank?.
I was trying to build my own blank? equivalent method but it's not easy. Here's my try:
def my_blank(state)
if state == nil or state.empty? == true
true
else
false
end
end
Upvotes: 0
Views: 416
Reputation: 11
Here is another variant I wrote to help me with some of the structures in our system:
Object.class_eval do
def deep_blank?
self.kind_of?(Enumerable) ? self.inject(true) { |running, element| running &&= element.kind_of?(Enumerable) ? element.deep_blank? : element.blank? } : self.blank?
end
end
Hope it helps someone.
Upvotes: 1
Reputation: 132257
This project is open source, so just take a look at the source: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/blank.rb
You'll see that there are individual methods written for the various classes (like String
, Array
, etc.)
Upvotes: 6