ternary-operator

DRY up Ruby ternary

谁说我不能喝 提交于 2019-12-06 23:07:16
问题 I often have a situation where I want to do some conditional logic and then return a part of the condition. How can I do this without repeating the part of the condition in the true or false expression? For example: ClassName.method.blank? ? false : ClassName.method Is there any way to avoid repeating ClassName.method ? Here is a real-world example: PROFESSIONAL_ROLES.key(self.professional_role).nil? ? 948460516 : PROFESSIONAL_ROLES.key(self.professional_role) 回答1: Assuming you're okay with

question mark and colon - if else in ruby

孤人 提交于 2019-12-06 23:05:59
问题 Hi I have a question about ruby on rails Apparently I have a statement like this: def sort_column Product.column_names.include?(params[:sort]) ? params[:sort] : "name" end From what I read, it's said that this method sort the column based on params[:sort] and if there no params the products will be sorted by "name". However, I don't understand the way this statement is written, especially the second "?". Can someone explain it to me ? 回答1: This is your code, rearranged for easier

Does VHDL have a ternary operator?

[亡魂溺海] 提交于 2019-12-06 18:51:58
问题 I love the neatness of the ternary operator vs if clauses. Does this operator exist in vhdl? My search was to the contrary. I also checked the when statement out, but it's not an operator, and I want to be able to use it in processes, too... 回答1: No. It was discussed for VHDL-2008, but didn't get in. You've got a couple of options. If your tools support VHDL-2008, conditional assignments are now supported as sequential statements (they were previously just concurrent), so you can write

Ternary Operator syntax to choose implementation of Interface [duplicate]

人盡茶涼 提交于 2019-12-06 18:17:46
问题 This question already has answers here : Implicit conversion issue in a ternary condition [duplicate] (4 answers) Closed 5 years ago . I am wondering why this line of code doesn't compile: ILogStuff Logger = (_logMode) ? new LogToDisc() : new LogToConsole(); Note that both classes LogToDisc and LogToConsole implement ILogStuff , and _logMode is a boolean variable. The error message I get is: Error 3: Type of conditional expression cannot be determined because there is no implicit conversion

Quickest PHP equivalent of javascript `var a = var1||var2||var3;` expression

允我心安 提交于 2019-12-06 18:00:55
问题 Firstly is there a name for this expression ? Javascript var value = false || 0 || '' || !1 || 'string' || 'wont get this far'; value equals string (string) aka the fifth option PHP $value = false || 0 || '' || !1 || 'string' || 'wont get this far'; $value equals true (bool) Am I right in thinking the correct way to achieve the same result as JavaScript is by nesting ternary operators? What is the best solution ? 回答1: The equivalent operator in PHP is ?: , which is the ternary operator

Delphi - Equivalent to C#'s ternary operator? [duplicate]

核能气质少年 提交于 2019-12-06 16:43:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is there, or is there ever going to be, a conditional operator in Delphi? I understand Delphi doesnt have the ternary operator as in C#. i.e. ?: So how best to represent this function call? Whats the cleanest method out there? Would be very nice if there is any code out there that can be used INSTEAD of writing a separate function? If not, whats the most efficient and/or cleanest code representation of it? 回答1:

Ternary Operators in Scala

假如想象 提交于 2019-12-06 16:36:38
I would like to simplify this: var countA: Int = 0 var countB: Int = 0 if (validItem) { if (region.equalsIgnoreCase( "US" )) { if (itemList > 0) { countB = 1 } else { countA = 1 } } else { countB = 1 } } else { countA = 1 } How do I use ternary operator in scala. jwvh This might be a bit confusing for a "newbie", but you could attach a ternary method to the Boolean class like so. implicit class Ternary[T](condition: Boolean) { def ??(a: => T, b: => T): T = if (condition) a else b } Usage: (4 == 4)??("yes","no") // res0: String = yes ("abc".length < 2).??(1,0) // res1: Int = 0 List('c').isEmpty

PHP ternary operator error [duplicate]

梦想与她 提交于 2019-12-06 13:34:13
This question already has answers here : Closed 6 years ago . Possible Duplicate: PHP ternary operator not working as expected I dont know what is wrong with my code? My PHP Version is 5.4.7. $b = 'a'; $c = 'd'; echo $b == 'a' ? 2: $c == 'a' ? 1 : 0; output 1 right answer should be 2..... Thank you very much for your advice. You need to add some parenthesis. $b = 'a'; $c = 'd'; echo ($b == 'a') ? 2 : ($c == 'a' ? 1 : 0); 来源: https://stackoverflow.com/questions/14399745/php-ternary-operator-error

Using ternary operator on Console.WriteLine

别来无恙 提交于 2019-12-06 11:57:27
I need to print some string based on the true or false of a condition. For example: if(i == m) { Console.WriteLine("Number is valid"); } else { Console.WriteLine("Number is invalid"); } How can I check this condition and print a message using conditional operator and with only one Console.WriteLine ? I was trying: (i == m) ? Console.WriteLine("Number is valid") : Console.WriteLine("Number is not valid"); I know I'm doing it wrong here. Can someone please tell me the correct way? Try this: Console.WriteLine("Number is " + ((i == m) ? "valid" : "not valid")); Move your ternary operation inside

How to I execute multiple functions on the result of a ternary operation?

為{幸葍}努か 提交于 2019-12-06 09:25:33
I have an if/else statement that results in two functions being called if it evaluates as true. if (isTrue) { functionOne(); functionTwo(); } else { functionThree(); } I would like to be able to put that in a ternary statement like this: isTrue ? (functionOne(), functionTwo()) : functionThree(); Is this possible? Your example is indeed valid javascript. You can use a comma to separate expressions , and wrap that in a single statement with parentheses for the ternary. var functionOne = function() { console.log(1); } var functionTwo = function() { console.log(2); } var functionThree = function()