Skip to content
Tags

,

.nil? .exists? and .present?

March 21, 2011

The opposite of .nil? is not .exists? but .present?

ruby-1.9.2-p136 :012 > Product.first.exists?

NameError: undefined local variable or method `exists?' for #<Product:0x0000010c00c708>	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/gems/activemodel-3.0.5/lib/active_model/attribute_methods.rb:364:in `method_missing'	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.5/lib/active_record/attribute_methods.rb:46:in `method_missing'	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/bundler/gems/spree_related_products-1c0dd3ebca7b/lib/spree_related_products.rb:23:in `method_missing'	from (irb):12	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'	from /Users/gagan/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'	from script/rails:6:in `require'	from script/rails:6:in `<main>'

ruby-1.9.2-p136 :013 > Product.first.present?

=> true

ruby-1.9.2-p136 :014 > nil.present?

=> false

But take a note that:

ruby-1.9.2-p0 :006 > "".present?
 => false 
ruby-1.9.2-p0 :007 > "".nil?
 => false
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: