[Ruby] present/blank method and postfix if.

This is a personal memo.

By using the if statement and the present? Or blank? Methods, the process can be branched depending on whether the value is set in the variable.

In ruby, if can be written after (called postfix if). You can use this to make a simple description.

present? and blank?

Object.present? Returns true if object has a value.

Object.blank? Returns true if object has no value or is empty. There is no value for nil and empty ("").

If there is no value


irb(main):333:0> x
=> nil

irb(main):333:0> x.present?
=> false

irb(main):334:0> x.blank?
=> true

If there is a value


irb(main):335:0> test
=> "test"

irb(main):336:0> test.present?
=> true
irb(main):337:0> test.blank?
=> false

Supplement

There are also nil? And empty? Methods that are similar to blank ?. The relationship between the object to be verified and the output is as follows.

Method nil empty ("")
blank? true true
nil? true false
empty false true
present? false false

If the value is empty


irb(main):340:0> z = ""
=> ""
irb(main):341:0> z.empty?
=> true
irb(main):342:0> z.nil?
=> false

## Postfix if Apply a conditional expression after the process. Line breaks and end in the conventional description can be omitted.

Processing if conditional expression

Postfix if


def jadge(x)
  "exist" if x.present?
end

Normal if statement


def jadge(x)
  if x.present?
    "exist"
  end
end

## Conditional bifurcation depending on the presence or absence of a variable value Conditional bifurcation is possible by using blank or present. You can write it clearly by using the postfix if.

python


def jadge(x)
  y = "No" if x.blank?
  y = "Exist" if x.present?
  p "the answer#{y}is"
end

Recommended Posts

[Ruby] present/blank method and postfix if.
[Ruby] postfix if
[Ruby basics] split method and to_s method
[Ruby] If and else problems-with operators-
ruby Exercise Memo II (variable and method)
Ruby to_s method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
variable and method
[Ruby] initialize method
Ruby build method
Ruby and Gem
Ruby accessor method
ruby map method
Ruby if, case
[Ruby] How to use gsub method and sub method
If it is Ruby, it is efficient to make it a method and stock the processing.
[Ruby] Classes and instances
Ruby Learning # 30 Initialize Method
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
Ruby Learning # 17 If Statements
About Ruby if statement
definition of ruby method
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
[Ruby] Get a 3-digit integer and conditionally branch using an if statement (digits method)
Be careful about Ruby method calls and variable references
[Ruby] Method definition summary
[Ruby] Questions and verification about the number of method arguments
Ruby on Rails record search, create if not find_or_create_by method
About for statement and if statement
GraphQL Ruby and actual development
[Rails] require method and permit method
Ruby Learning # 18 If Statements (Con't)
Rails "render method" and "redirect method"
Ruby syntax errors and countermeasures
Java if and switch statements
Integer check method with ruby
Ruby algorithm (inject, method definition)
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
Java methods and method overloads
[Ruby] Notes on gets method
[Ruby] problem with if statement
About Ruby and object model
[Ruby] Method that returns truth
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
[ruby] Method call with argument
[Ruby] Singular methods and singular classes
[Ruby] conditional branch if statement
Easy to understand the difference between Ruby instance method and class method.