The Question Mark Operator in Ruby on rails

By: Brian Marick Emailed: 1589 times Printed: 2037 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

You’ll often find yourself selecting between two alternative values for a variable. That can be done like this:

if input < 0

output = 0

else

output = input

end

But it seems wrong to take up five lines for such a simple idea. You can reduce it to two by picking one of the values and then possibly overriding it:

output = input

output = 0 if input < 0

But it’s a little confusing for the code to do something and then immediately say, “Wait! I didn’t mean that!” So there’s a compact version of if for just this purpose:

output = (input < 0) ? 0 : input

You can read that as “if input is less than zero, then return 0, else return whatever object input names.”

This ?: construct is called either the question mark operator or, more question mark operator often, the ternary operator. (“Ternary” because it uses three expres- ternary operator sions, unlike operators such as +, which have two and are called binary operators.) binary operators


Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Errm, Rails is just a bunch of Ruby scripts, not a language itself.
The ternary operator you're describung is part of Ruby, not Rails.


View Tutorial          By: gngn at 2010-07-12 22:19:55

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Brian Marick
The Question Mark Operator in Ruby on rails
unless in Ruby on rails
if . . . elsif . . . else in Ruby on rails
Methods in Ruby on rails
Command-line Arguments in Ruby on rails
Naming conventions in Ruby on rails

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
encoding and decoding in Ruby using Base64 Module
Open and manipulate CSV files in Ruby
Reading emails using POP3 in Ruby
if . . . elsif . . . else in Ruby on rails
Module Abbrev in Ruby
Benchmark module in Ruby
URL encode a string in Ruby
Using Proxy to connect to URLs in Ruby
Sending emails using SMTP in Ruby
Action Pack - Ruby On rails from request to response
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
The Question Mark Operator in Ruby on rails
Standard Library Packages in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
Most Emailed Articles (in Ruby)
header() in Ruby
unescapeElement() in Ruby
What is dRuby?
Command-line Arguments in Ruby on rails
The Question Mark Operator in Ruby on rails
POST a form in Ruby
Reading emails using POP3 in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
Open and manipulate CSV files in Ruby
Sending emails using SMTP in Ruby
Using Observer in Ruby
Action Pack - Ruby On rails from request to response
Methods in Ruby on rails