Thanks for all the fish
Thanks for all the fish

Reputation: 1701

Rails 3.2 - How Rails blank? method works internally?

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

Answers (2)

Alex Sherstinsky
Alex Sherstinsky

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

Peter
Peter

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

Related Questions