Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Null-Conditional operator [duplicate]

Tags:

ruby

C# has a null-conditional operator that works like this

SomeObject?.SomeParam

If SomeObject is null, then the result of that expression will be null rather than throwing a null reference exception. Does something like this exist in Ruby?

like image 445
Nashenas Avatar asked Jan 22 '26 05:01

Nashenas


1 Answers

Yes. Use &. to call a method.

some_value&.some_method

If some_value is nil, then some_method will not be executed, and the return value of the expression will be nil. Otherwise, some_method will be called as with when called with ..

like image 65
sawa Avatar answered Jan 25 '26 20:01

sawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!