Skip to content

Instantly share code, notes, and snippets.

@fredjean
Created December 29, 2009 03:40
Show Gist options
  • Save fredjean/265132 to your computer and use it in GitHub Desktop.
Save fredjean/265132 to your computer and use it in GitHub Desktop.
class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, "", " ", +nil+, [], and {} are blank.
#
# This simplifies
#
# if !address.nil? && !address.empty?
#
# to
#
# if !address.blank?
def blank?
respond_to?(:empty?) ? empty? : !self
end
# An object is present if it's not blank.
def present?
!blank?
end
en
>> a = []
=> []
>> a.empty?
=> true
>> a.blank?
=> true
>> a[0] = 1
=> 1
>> a.blank?
=> false
>> x = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.lang.String"), 0)
=> #<#<Class:01x25616d8d>:0x6f6b99ca @java_object=#<Java::JavaArray:0x13dd5ec>>
>> a = []
=> []
>> x.class
=> #<Class:01x25616d8d>
>> x.class.ancestors
=> [#<Class:01x25616d8d>, ArrayJavaProxy, Enumerable, JavaProxy, JavaProxyMethods, Object, JSON::Pure::Generator::GeneratorMethods::Object, NewRelic::Agent::Instrumentation::ErrorInstrumentation::Shim, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, ActiveSupport::Dependencies::Loadable, Base64::Deprecated, Base64, Kernel]
>> a.class
=> Array
>> a.class.ancestors
=> [Array, JSON::Pure::Generator::GeneratorMethods::Array, JSON::Ext::Generator::GeneratorMethods::Array, ActiveSupport::CoreExtensions::Array::RandomAccess, ActiveSupport::CoreExtensions::Array::Grouping, ActiveSupport::CoreExtensions::Array::ExtractOptions, ActiveSupport::CoreExtensions::Array::Conversions, ActiveSupport::CoreExtensions::Array::Access, Enumerable, Object, JSON::Pure::Generator::GeneratorMethods::Object, NewRelic::Agent::Instrumentation::ErrorInstrumentation::Shim, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, ActiveSupport::Dependencies::Loadable, Base64::Deprecated, Base64, Kernel]
>>
>> x = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.lang.String"), 0)
=> #<#<Class:01x113ee167>:0x37403a09 @java_object=#<Java::JavaArray:0x59a24135>>
>> x.respond_to? :empty?
=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment