Irb and rails console show different results for [] .blank?

why irb and rails console show different results for [] .blank?

Here is my irb check:

$ irb
2.1.0 :001 > a = []
 => [] 
2.1.0 :002 > a.blank?
NoMethodError: undefined method `blank?' for []:Array
    from (irb):2
    from /home/user/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'

      

Here is my console check:

$ rails c -e local
Loading local environment (Rails 4.1.5)
2.1.0 :001 > a = []
 => [] 
2.1.0 :002 > a.blank?
 => true 

      

+3


source to share


2 answers


ActiveSupport extension required

Object # blank method ? is part of the Rails ActiveSupport Core Extensions . By default, Rails includes ActiveSupport. However, these extensions can also be installed independently of Rails as a gem .



You can include ActiveSupport parts in other applications or in interactive REPL sessions, requiring you to need ActiveSupport parts. For example, to mix #blank support? Method:

require 'active_support'
require 'active_support/core_ext/object/blank'

[].blank?
# => true

      

+2


source


the method is blank?

added by rails (not in the standard ruby ​​language) From the rails docs: http://api.rubyonrails.org/



# File activesupport/lib/active_support/core_ext/object/blank.rb, line 15
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

      

+6


source







All Articles