سؤال

Why doesn't this statement work?

1>2 ? puts "true" : puts "false" 

Here, I find that most ruby operators are like C with () parentheses having high priority. This code In C

1 > 2 ? printf("true") : printf("false")

executes successfully. Why is ruby code not working?

هل كانت مفيدة؟

المحلول

The error indicates that ternary operator has lower priority than method argument. Ruby parses around the first instance of puts method up to:

puts "true"

and looks if there is another argument, which should be preceded by a comma if there is any. But you have a colon continuing:

 : puts "false"

which cases a syntax error.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top