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 8818809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:11:56+00:00 2026-06-14T05:11:56+00:00

There was a weird thing I encountered in Ruby this morning, concerning the ternary

  • 0

There was a weird thing I encountered in Ruby this morning, concerning the ternary operator. Here how it goes:

x = nil ? x : true 

As known to all, x is set to true, no surprise there. Now, subsequentlyl running:

defined?(y) 
=> nil

This answer would imply y has not been defined yet. However:

defined?(y) ? y : true

Returns true. No surprise again. The surprise comes here:

y = defined?(y) ? y : true 

and what happens? y is set to nil!

But wait, there is more. Now since y is assigned, let’s work with z:

defined?(z) 
#=> nil

Implying z is not defined yet.

z = defined?(z) ? false : true

And the suprise: z is set to false. I have no idea how that happens. Doing the same in an if block gives the same result.

z1 = if defined?(z1) then z1 else true end

Again z1 is set to nil.

z2 = if defined?(z2) then false else true end

This, too, gives me a surprise, as z2 is set to false. Now I was assuming the above expression to behave something like:

z3 = if nil then false else true end

where z3 gets assigned to true, considering the fact that defined? returned nil in all the above cases. This makes me believe that there is something special at work around defined? call, but I could find no information on it in Ruby documentation.

Btw. I tested the above on ruby 1.8.7 and 1.9.2

  • 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-06-14T05:11:57+00:00Added an answer on June 14, 2026 at 5:11 am

    Ok. I think this explains it:

    ccc = (ccc == nil)
    => true
    

    Apparently, whenever you assign something to a variable, ruby first initializes it to nil.

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

Sidebar

Related Questions

There is a weird code here that I need to make work.. can you
I'm reading code from GraphServer . And there is this function that has weird
I am building a website using App Engine and weird thing just happened. There
I realize this is sort of a weird thing to be doing, but I
I am a rookie. Recently, there is a weird thing happens to my site
I have this weird thing happening: when clicking spans in the navigation the content
I have this weird thing happening with my testcase, however rather than fixing it
So, this might be a weird thing to try to do, but I'm curious
I found this pretty weird thing when I first installed SQL Server 2008, for
This is a weird thing I've been encountering and I was wondering if people

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.