Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby's equivalent to C#'s ?? operator [duplicate]

Possible Duplicate:
C# ?? operator in Ruby?

Is there a Ruby operator that does the same thing as C#'s ?? operator?

The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

from http://msdn.microsoft.com/en-us/library/ms173224.aspx

like image 769
Shuo Avatar asked Jan 09 '11 03:01

Shuo


People also ask

Is Ruby similar to C?

Ruby is much much simpler than C++—it will spoil you rotten. Ruby is dynamically typed, rather than statically typed—the runtime does as much as possible at run-time. For example, you don't need to know what modules your Ruby program will “link to” (that is, load and use) or what methods it will call ahead of time.

How do we write Ruby in C?

Writing A Ruby Method From C A VALUE is a Ruby object. Now we need to attach this function to a Ruby class or module. You can create a new class or use an existing one. You can use the rb_define_method method to attach the C function to this module.

Is Ruby based on C?

The original Ruby interpreter is often referred to as Matz's Ruby Interpreter or MRI. This implementation is written in C and uses its own Ruby-specific virtual machine. The standardized and retired Ruby 1.8 implementation was written in C, as a single-pass interpreted language.

What are the Ruby objects in C?

This is how Ruby implements object-oriented code in C: a Ruby object is an allocated structure in memory that contains a table of instance variables and information about the class. The class itself is another object (an allocated structure in memory) that contains a table of the methods defined for that class.


1 Answers

The name of the operator is the null-coalescing operator. The original blog post I linked to that covered the differences in null coalescing between languages has been taken down. A newer comparison between C# and Ruby null coalescing can be found here.

In short, you can use ||, as in:

a_or_b = (a || b) 
like image 195
Chris Pitman Avatar answered Sep 28 '22 09:09

Chris Pitman