Syntax

Ruby Safe Navigation Syntax

In Ruby, safe navigation: calls a method only if the object isn't nil — user&.name

Quick Answer

Syntax
user&.name
RubySyntax

Safe Navigation

Code example
user&.name

Calls a method only if the object isn't nil.

Learn More

How to safe navigation in Ruby

  1. Open your Ruby file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Ruby Safe Navigation Syntax

Use user&.name.

Related Syntax References

Frequently asked questions

What is the Ruby syntax for safe navigation?

Use this pattern: user&.name

Can I copy this Ruby code example?

Yes. Use Copy code and adapt the example to your program.

Is this Ruby syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify