I've been following the exercises on RubyMonk and at least 2 exercises so far use a question mark. This symbol is not at the end of empty? or include? etc. It's not even being used in a regex. When I can't figure something out, I get frustrated. I've given up on a few of these exercises and had to hit the option "See the solution". It was even more frustrating when the solution uses syntax not introduced in the lesson or even hinted at on the page asking you to solving a problem. RubyMonk is not the only site that has this problem. Had to vent...sorry.
Some Ruby beginners but most intermediate and all advanced will know that what I speak of above is the ternary or conditional operator. I have been googling Ruby question mark and adding words like symbol and comparison. Right now, I'm working on the problem named Number Shuffle. I had to click on the "See the solution" option which is the second time I'm seeing a question mark. Here's the specific line...
no_of_combinations = number.to_s.size == 3 ? 6 : 24
Searching for this kept sending me to pages that discussed array/string elements ending with a question mark or pages discussing regular expressions. I finally found a page that talks about [value] ? [value] : [value] I guess I wasn't using the right search terms or something.
I found the explanation on this page...
http://ruby.about.com/od/beginningruby/a/The-Ternary-Or-Conditional-Operator.htm
Here is the text that set me straight and answered my question...
The general format of this expression is as follows: conditional ? true : false. If the conditional expression is true, then the operator will evaluate as the true expression, otherwise it will evaluate as the false expression.
Now that I found out that this is a ternary operator, I can google and find all kinds of explanations on it. It would be nice if online tutorials explained or gave references to all syntax used.
No comments:
Post a Comment