0
Tenary Operator (RUBY)
How do they work in ruby? I'm getting an error foo = hitchhiker ? 42 : 3.1415 // Assign result of ?: to a variable puts(hitchhiker ? 42 : 3.1415) // Pass result as argument return hitchhiker ? 42: 3.1415 // Return result
2 odpowiedzi
+ 3
They work like you typed them, except for the last one. You need a function to use return, and hitchhiker needs to be true or false:
hitchhiker = true
foo = hitchhiker ? 42 : 3.1415 # Assign result of ?: to a variable
puts(hitchhiker ? 42 : 3.1415) # Pass result as argument
#return hitchhiker ? 42: 3.1415 # Return result
puts(foo)
+ 2
Conditional (ternary) operator - takes three operands: a condition followed by a question mark (?), then an expression that executes if the condition is true is followed by a colon (:), and finally an expression that executes if the condition is false