[Ruby] What is `!!` used for?

Introduction

It doesn't come out even if I go around ... There is a sense of discomfort in using Japanese that seems to be missing ...

I will write it with my own interpretation that I looked up and saw

! Is a logical operator

A logical operator is an operator that returns true or false.

Other logical operators include ʻand and ʻor

! Evaluates the object

Returns true if nil or false

Returns false if true

That is !

That is, the logical operator of negation

Example

irb(main):018:0> !nil => true irb(main):019:0> !false => true irb(main):020:0> !true => false


#### ~~ `!!` is a negative logical operator of negation ~~

 * @scivola commented that the single logical operator `!!` is not open. The above does not seem to be an appropriate expression, so I will correct it, thank you! *

#### What is `!!`

 By repeating `!` Twice, the true or false returned by the first `!` Is denied once more.

 Returns false if nil or false

 Returns ture if true

 In other words, you are denying denial.
 > Example

>```
irb(main):025:0> !!false
=> false
irb(main):026:0> !!nil
=> false
irb(main):027:0> !!true
=> true

Well, I don't know what it's used for.

Isn't it missing? Do you feel that you are using Japanese like this?

The answer is logical operators. Was that

Logical operators evaluate and return logical value objects (true, false)

Of course, the return values of all objects are not necessarily logical value objects.

irb(main):001:0> a = "Ah"
=> "Ah"
irb(main):002:0> a
=> "Ah"

So, when you want true or false, if you write without using !!, it will look like this

irb(main):028:0> a = "Ah"
=> "Ah"
irb(main):029:0> a ? true : false
=> true

But if you use !!

irb(main):031:0> a = "Ah"
=> "Ah"
irb(main):032:0> !!a
=> true

With this, you can call with two letters! Amazing smart! !!

This was my merit ~ That's it!

Recommended Posts

[Ruby] What is `!!` used for?
[Ruby] What is true?
What is a Ruby module?
[Ruby] What is an instance?
What is the constructor for?
[Ruby on Rails] What is Bcrypt?
What is a Ruby 2D array?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
[Technical memo] What is "include" in Ruby?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
[For beginners] Ruby is said to be ruby, but what about it?
What is SLF4J?
[For super super beginners] What is object orientation?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is the Facade pattern useful for?
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
[Ruby] What happens if the method self is used as the return value?
What is the volatile modifier for Java variables?
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
abbreviation for ruby method
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?