[Ruby] Rails existence confirmation method, properly selected memo (any?/empty?/present?(!blank?)/nil?)

3 minute read

Introduction

In the Rails view, there was a scene where I wrote a process like “display an image if it exists”. There are several methods for checking existence, including those for Ruby and Rails. I did a little research, and when I selected the method “OK, let’s go!” and put it in the review, it was crushed as much as possible, so I will leave it as a memorandum so that I will not make a mistake in the future.

Environment

macOS Catalina Version 10.15.5 Ruby: 2.6.5 Ruby on Rails: 6.0.3

Survey target

  • any? (Ruby)
  • empty? (Ruby)
  • nil? (Ruby)
  • present? (Rails)

Motivation

There are similar articles in the past, and in fact, in most cases, “Rubby’s true/false determination method (nil?/empty?/blank?/present?) was verified, and it turned out to be interesting”. After that, the reasons for posting this time are as follows.

  • Although the any? method was not mentioned in the above post, I also wanted to confirm the behavior of any? (because of the problem with arrays involved this time)
  • In the table of summary of the above post,
    -“Method to ask if ** exists ** (present? etc.. Returns true if there is something)”
    -“** Does not exist ** Method (empty? etc.; returns true if there is nothing) “
    was written side by side, but a table that aligns the direction of conditions such as “return XX when there is something” I wanted (a table that can compare present? and not empty?)
    * Of course, the merit of the above table is that it is easy to understand that present? and blank?

In addition, blank? Is excluded (because the definition of present? is !blank?).

Survey method

Use rails console.

test = XXX ← Various substitutions here

test.any?
!test.empty?
test.present?
!test.nil?

Investigation result

| | any? | !empty?
(deny of empty?) | present?
(!blank?)|!nil?
(deny of nil?) | |:-:|:-:|:-:|:-:|:-:|:-:|:-:| | Ruby/Rails | Ruby | Ruby | Rails | Ruby | |1
Number|NoMethodError| NoMethodError|true|true| |”foo”
String|NoMethodError|true|true|true< /font>| |{key: value}
Hash |true|true|true</ font>|true| |["foo"]
Array |true|true|true</ font>|true| |true|NoMethodError|NoMethodError|true|true| |false|NoMethodError|NoMethodError|false|true| |nil|NoMethodError|NoMethodError|false|false| |""
empty|NoMethodError|false|false|true| |" "
Half-width space|NoMethodError|***TRUE***|***FALSE*** |true| |{}
Empty hash |false|false|false |true| |[]
Empty array |false|false|false |true| |[nil]
Array (element is nil)|***FALSE***|***TRUE***< /font>|***TRUE***|***TRUE***|

  • any? is defined in the Enumerable module, so if you try to use it other than an array or hash, it will result in NoMethodError.

The point

From this result, what I think should be noted in the future is highlighted in capital letters in the table, but I will write it again below.

  1. Half-width space is considered as existing thing is not empty** in !empty? and returns true, and in present? it is considered as empty and returns false
    ↑ (Fixed) I have received a comment from @scivola, corrected and added it. Thank you very much.
    (Addition) Note that present? (Motoiblank?) has a wider range of blank judgment
    Example: In addition to false, nil, half-width spaces, full-width spaces, tabs (\t), line feeds (\n), Unicode (\unnnn), etc. are also determined to be empty.
  2. Applying any? to the array returns true if there is a true element (=non-nil element). Other methods return true if there is an element, regardless of whether it is true or not.
  3. If you ask !nil? to the bare nil, it returns false, but if you ask !nil? for the array made with [nil], it will return true.

Other

In Rails, there is also a method called exists?. This is a method defined in ActiveRecord::Base, which seems to be used to check whether data with a specific condition exists in the database. I didn’t know about that, I initially included it in the survey, but all returned NoMethodError.

Learn

This is Qiita’s first post. I think it’s simple and quite natural, but it was a good opportunity to deepen understanding through the process of examining and organizing for output. I would like to regularly post articles that will be useful to me (≒others) in the future.

Reference URL

As a result of verifying the ruby’s true/false determination method (nil?/empty?/blank?/present?), it turned out to be interesting

Tags: ,

Updated: