I want to make sure I'm using them for the correct occasion and want to know of any subtleties. They seem to function the same way, which is to check to see if a object field has been defined, when I use them via the console and there isn't a whole lot information online when I did a google search. Thanks!
Chromium (Cr) metal is present in Ruby as an impurity. Ruby has pink to blood-red colour. It is a gemstone. It is a variety of the mineral corundum (aluminium oxide).
class Object # An object is blank if it's false, empty, or a whitespace string. # For example, +false+, '', ' ', +nil+, [], and {} are all blank. # # This simplifies # # ! address || address.empty? # #
In Ruby, nil is a special value that denotes the absence of any value. Nil is an object of NilClass. nil is Ruby's way of referring to nothing or void.
You’re calling the ‘+’ method on the ‘2’ object, and passing the other ‘2’ as an argument. So, even when expressing such a simple idea, Ruby still keeps loyal to its object-oriented ideas. Everything is an object. But what about nothing?
This one, like the previous method, isn’t native to the Ruby language itself, but provided by the Rails framework. This method is, by far, the easiest of all to understand. Here it goes: “present?” is just the negation of “blank?”.
Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code. For example − This may be more quickly declared using parallel assignment −
Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand. Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Ruby also supports the parallel assignment of variables.
To clarify: neither present?
nor exists?
are "pure" ruby—they're both from Rails-land.
present?
is an ActiveSupport extension to Object
. It's usually used as a test for an object's general "falsiness". From the documentation:
An object is
present
if it’s notblank?
. An object isblank
if it’sfalse
, empty, or a whitespace string.
So, for example:
[ "", " ", false, nil, [], {} ].any?(&:present?) # => false
exists?
is from ActiveResource. From its documentation:
Asserts the existence of a resource, returning true if the resource is found.
Note.create(:title => 'Hello, world.', :body => 'Nothing more for now...') Note.exists?(1) # => true
The big difference between the two methods, is that when you call present?
it initializes ActiveRecord for each record found(!), while exists?
does not
to show this I added after_initialize on User. it prints: 'You have initialized an object!'
User.where(name: 'mike').present?
User Load (8.1ms) SELECT "users".* FROM "users" WHERE "users"."name" = $1 ORDER BY users.id ASC [["name", 'mike']] You have initialized an object! You have initialized an object!
User.exists?(name: 'mike')
User Exists (2.4ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = $1 ORDER BY users.id ASC LIMIT 1 [["name", 'mike']]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With