Ruby Tricks, Idiomatic Ruby, Refactorings and Best Practices

Conditional assignment.

Ruby is heavily influenced by lisp. One piece of obvious inspiration in its design is that conditional operators return values. This makes assignment based on conditions quite clean and simple to read.

There are quite a number of conditional operators you can use. Optimize for readability, maintainability, and concision.

If you're just assigning based on a boolean, the ternary operator works best (this is not specific to ruby/lisp but still good and in the spirit of the following examples!).

If you need to perform more complex logic based on a conditional or allow for > 2 outcomes, if / elsif / else statements work well.

If you're assigning based on the value of a single variable, use case / when A good barometer is to think if you could represent the assignment in a hash. For example, in the following example you could look up the value of opinion in a hash that looks like {"ANGRY" => comfort, "MEH" => ignore ...}

My Profile Photo

Chris Arcand

Principal Engineer building Terraform at HashiCorp 👨‍💻 Formerly: Red Hat , NBC SportsEngine , RubyMN organizer. Once played the clarinet for a living, now making a living to play hockey. I make pointless connections to Minnesota as a Minnesotan does.

St. Paul, MN

Null Coalescing Operators and Ruby's Conditional Assignments

A long time ago in a galaxy far, far away (not really) I started learning and writing Ruby professionally by diving in to an existing Rails project. Learning in a ‘trial by fire’ sort of setting with other developers more experienced with a language is a great way to pick something up very quickly. I remember the first time coming across something similar to the following:

This is probably the most basic and ubiquitous form of memoization in Ruby. In this case, I was told that with the combination of the ||= operator and Ruby’s implicit return this means:

Assign @something to a new Something object if it isn’t already initialized and return it, otherwise return the preexisting value of @something

It’s a simplistic answer but a sufficient one to tell a newcomer in the middle of a project. Having experience in other languages, I thought to myself Ah! Ruby’s null coalescing operator! I wondered about it but it never really occurred to me why the syntax of such an operator was the double pipe ( || ). Shrug , move on.

After writing Ruby for a quite a while and having written various versions of this same pattern many, many times, I was recently burned by ||= . I had to very quickly come up with a toggle in a class and came up with this:

Note that not only is this a dumb use for using this memoization pattern (there are many ways to create such a toggle with a default value of true - don’t use this one that I rushed to), it also doesn’t work . After a very short investigation of the issue and actually taking a moment to rediscover this operator myself, I quickly realized my (perhaps obvious) misunderstanding.

True Null Coalescing Operators

A true null coalescing operator is “a binary operator that is part of the syntax for a basic conditional expression” where you can both specify a value to be evaluated which is returned if not null and a value to be returned if the first value is null. That mouthful can be seen in this pseudo-code:

‘NOT NULL’ could also just mean ‘undefined’. Some programming languages don’t have an implicit notion of NULL. Many languages implement null coalescing operators. Here are just a few examples:

Perl’s version is //

In C#, the operator is ??

Swift takes a cue from C# and also has ??

From PHP5 and on, you can leave out the middle part of a ternary operator to create a binary one.

Apparently future versions of PHP might have a true ?? operator like C# and Swift.

Groovy took PHP’s thunder and actually made a separate operator out of ?: , calling it the ‘Elvis operator’. Look at it like an emoji. See Elvis’s hair curl?

Of course, all of these examples are null coalescing without implicit assignment; We’re assigning $greeting based on the existence of $more_personal_greeting . In Perl (at least) there’s a variation on // that acts and looks even more like Ruby’s ||= where assigning and checking $greeting is done implicitly:

$greeting is assigned to $greeting if it’s defined (no change) or "Hello." otherwise.

Ruby’s Conditional Assignments

Although it’s often used like it, Ruby’s ||= is not a null coalescing operator but a conditional assignment operator . In my quickly written Ruby example, @some_toggle ||= true always returns true, even if @some_toggle was previously assigned to false .

The reason is because this operator, ‘Or Equals’, coalesces false as well as nil . In Ruby anything that isn’t false or nil is truthy. ‘OrEquals’ is a shortcut for writing the following:

Oh. So that’s why the syntax is ‘double pipe equals’, ||= . All this operator does is take Ruby’s easy || logic operator and combine it with the assignment operator = . In hindsight - after having a lot more experience with Ruby logic and logical operators in general - it makes perfect sense. It’s not wrong, it’s just not a true null coalescing assignment operator. Don’t fall victim!

A Ruby version of what we’re trying to do could look like this:

Lastly, saying ‘ ||= is a conditional assignment operator’ is only a subset of the larger truth. The mix of various operators and assignment is simply referred to as abbreviated assignment . You’ve probably seen this in the form of += to increment a counter or add to a string. In fact, you can mix any of the following operators in the pattern <operator>= : +, -, *, /, %, **, &, &&, |, ||, ^, <<, >> .

Enjoy this post? Please share!

IMAGES

  1. Rails Conditional Validation (2 Solutions!!)

    conditional assignment rails

  2. Solved conditional assignment Using a conditional

    conditional assignment rails

  3. How can we do conditional through in has_many association in rails

    conditional assignment rails

  4. Rails 3.2 way to count conditional associations without 1+n queries (2

    conditional assignment rails

  5. Conditional Assignment

    conditional assignment rails

  6. HTML : Rails

    conditional assignment rails

VIDEO

  1. 3.1: Introduction to Conditional Statements

  2. 13

  3. Advanced Conditional Formatting in Google Sheets

  4. MS Excel

  5. Excel Conditional Formating

  6. Conditional Probability

COMMENTS

  1. One-line assignment: If (condition) variable = value1 else value2

    if params[:property] == nil. @item.property = true. else. @item.property = false. Always forget the proper syntax to write it in one line. In PHP it would be like this: @item.property=(params[:property]==nil)true:false. Is it the same in rails? ruby-on-rails.

  2. Rails

    I am starting with rails, and I can't write this on a rails syntax. Can I use the variable like this? How can I compare 2 variables with the true, false format?

  3. Conditional assignment

    Conditional assignment. Ruby is heavily influenced by lisp. One piece of obvious inspiration in its design is that conditional operators return values. This makes assignment based on …

  4. Null Coalescing Operators and Ruby's Conditional Assignments

    Ruby’s Conditional Assignments. Although it’s often used like it, Ruby’s ||= is not a null coalescing operator but a conditional assignment operator. In my quickly written Ruby …

  5. Active Record Associations

    Active Record Associations. This guide covers the association features of Active Record. After reading this guide, you will know how to: Understand the various types of associations. Declare associations between Active Record models. …

  6. Conditionals in Ruby (Example)

    Conditionals allow your code to take different paths. Learn how to use conditionals like if statements in your Ruby code.

  7. Active Record Callbacks

    After reading this guide, you will know: When certain events occur during the life of an Active Record object. How to register, run, and skip callbacks that respond to these events. How to create relational, association, …