Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ?? operator in Ruby?

Is it possible to implement the ?? operator in Ruby?

a = nil b = 1  x = a ?? b # x should == 1 x = b ?? 2 # x should == 1 
like image 350
Ryan Avatar asked Jun 04 '09 21:06

Ryan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

In Ruby, the short-circuiting Boolean operators (||, &&, and and or) do not return true or false, but rather the first operand that determines the outcome of the entire expression. This works, because Ruby has a rather simple idea of truth. Or rather, it has a rather simple idea of falsehood: nil is false, and obviously false is false. Everything else is true.

So, since || is true when at least one of its operands is true, and operands are evaluated from left to right, this means that a || b returns a, when a is true. But when a is false, then the outcome of the expression is solely dependent on b, and thus b is returned.

That means that, because nil is false, you can just use || instead of ?? for the examples that you gave. (There is also the nifty a ||= b operator, which kind of works like a || a = b, but not quite.)

However, that only works, because you don't use Booleans in your examples. If you expect to deal with Boolean values, that won't work:

b = false  x = b || 2 # x should be == false, but will be 2 

In that case, you will have to use #nil?, and a conditional expression:

b = false  x = unless b.nil? then b else 2 end # x should be == 2 

or using the ternary conditional operator:

b = false  x = b.nil? ? 2 : b # x should be == false 

If you want to, you can wrap that up in a nice method:

class Object   def _? b = nil     return self   end end  class NilClass   def _? b = nil     return yield if block_given?     return b   end end  b = false  x = b._? { 2 } # x should be == false x = b._? 2 # x should be == false 

This cute snippet brought to you by polymorphism, open classes and the fact that nil is actually an object representing nothingness (unlike, say, Java, where null is actually nothing).

like image 149
Jörg W Mittag Avatar answered Oct 04 '22 16:10

Jörg W Mittag


You're looking for conditional assignment:

a ||= b  # Assign if a isn't already set 

and the || operator

a = b || 2 # Assign if b is assigned, or assign 2 
like image 42
jonnii Avatar answered Oct 04 '22 17:10

jonnii