[RUBY] Try using || instead of the ternary operator

Introduction

I used to implement it in Ruby using the ternary operator,||I think that using is shorter and more readable. (Personally)

||Let's use

For example, suppose you want to assign the value a to a variable x if the value a is not nil, and 1 if the value a is nil. In that case, the ternary operator can be implemented as follows.

x = !a.nil? ? a : 1

||Can be implemented as follows.

x = a || 1

||Then, since it is evaluated in order from the left, the evaluation target is false(nil or false)If so, move to the right and return the first true one.

What do you think. I think there are likes and dislikes, but in the above cases,||I think it's cleaner to use.

Recommended Posts

Try using || instead of the ternary operator
About the ternary operator
Is the ternary operator evil?
The first step to using Xib instead of StoryBoard
Try using the query attribute of Ruby on Rails
[Java] Try editing the elements of the Json string using the library
Try using the messaging system Pulsar
The basics of Swift's TableView
Transition using the header image of CardView as it is
Try using || instead of the ternary operator
Try using the two-point measurement function of Firebase Performance Monitoring. [Android]
Try using the service on Android Oreo
Try using the Stream API in Java
Test the integrity of the aggregation using ArchUnit ②
Try using the Rails API (zip code)
How do you write the ternary operator (? :)
Try using the Emotion API from Android
Try using the Wii remote with Java
Test the integrity of the aggregation using ArchUnit ③
Try using libGDX
The story of intentionally using try catch for the first time in my life
Try using Maven
Try using powermock-mockito2-2.0.2
Assignment to multiple variables with the ternary operator
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Reference ternary operator
Try similar search of Image Search using Java SDK [Search]
Try accessing the dataset from Java using JZOS
Try using SwiftLint
Try using Log4j 2.0
Try the free version of Progate [Java II]
Try using the COTOHA API parsing in Java
I tried using the profiler of IntelliJ IDEA
Try the free version of Progate [Java I]
I tried using the Server Push function of Servlet 4.0
Try implementing the Eratosthenes sieve using the Java standard library
Rails6: Input the initial data of ActionText using seed
Output of the day of the week using Ruby's Date class
Try global hooking in Java using the JNativeHook library
Limit the number of threads using Java's Executor Service
Are you using the default method of the interface properly?
The story of making Dr. Oakid using LINE BOT
Command to try using Docker for the time being
Try using Axon Framework
The world of clara-rules (2)
Try using JobScheduler's REST-API
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
Try using Talend Part 2
Judgment of the calendar
The world of clara-rules (4)
Example of using vue.config.js
The world of clara-rules (1)
The world of clara-rules (3)
Summary of using FragmentArgs
The world of clara-rules (5)
The idea of quicksort
Try using Talend Part 1
Try using F # list
About the [ruby] operator