Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:51:57+00:00 2026-05-26T19:51:57+00:00

Experimenting with the conditional operator in ruby, def nada false ? true : nil

  • 0

Experimenting with the conditional operator in ruby,

def nada
  false ? true : nil
end

def err
  false ? true : raise('false')
end

work as expected but

def reflection
  false ? true : return false
end

produces a syntax error, unexpected keyword_false, expecting keyword_end

def reflection
  false ? true : return(false)
end

and attempted with brackets syntax error, unexpected tLPAREN, expecting keyword_end

yet

def reflection
  false ? true : (return false)
end

works as expected, and the more verbose if…then…else…end

def falsy
  if false then true else return false end
end

also works as expected.

So what’s up with the conditional (ternary) operator?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:51:58+00:00Added an answer on May 26, 2026 at 7:51 pm

    You can use it like this, by putting the entire return expression in parentheses:

    def reflection
      false ? true : (return false)
    end
    

    Of course, it does not make much sense used like this, but since you’re experimenting (good!), the above works! The error is because of the way the Ruby grammar works I suppose – it expects a certain structure to form a valid expression.

    UPDATE

    Quoting some information from a draft specification:

    An expression is a program construct which make up a statement (see 12
    ). A single expression can be a statement as an expression-statement
    (see 12.2).12

    NOTE A difference between an expression and a statement is that an
    expression is ordinarily used where its value is required, but a
    statement is ordinarily used where its value is not necessarily
    required. However, there are some exceptions. For example, a
    jump-expression (see 11.5.2.4) does not have a value, and the value
    of the last statement of a compound-statement can be used.

    NB. In the above, jump-expression includes return among others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Experimenting with Ruby's Benchmark module... >> Benchmark.bm(7) { |b| b.report('Report:') { s = ''
After experimenting with some triangulation work I ran across the question on how to
i've been reading and experimenting with conditional css to display the following css for
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
While experimenting with this question on collections in Spring.NET , I discovered that Spring
While experimenting with pixel shaders in WPF I decided to draw some pixels onto
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I experimenting with twitter streaming API, I use Phirehose to connect to twitter and
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I'm currently experimenting with build script, and since I have an ASP.net Web Part

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.