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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:51:41+00:00 2026-06-05T15:51:41+00:00

update: I mistyped 2 variables…so embarrassing. thanks everyone for the effort! sorry i find

  • 0

update: I mistyped 2 variables…so embarrassing. thanks everyone for the effort!

sorry i find I typed into wrong value for 2 different env, 1st one i set sort = 0 and rating=1 but then in irb I set sort=1 and rating=0. since for a and b when a=1 it only evaluates b so in the 2nd situation I get false. now it comes clear. sorry for the careless, thanks everyone for the effort!

======================

my question:
I have 2 variables, sort and rating, but both sort&&rating == 1 and sort and rating ==1 behave differently in 2 different env as below:

1st, in the debugger env of rails when I execute rails server --debug, they all return true:

(rdb:130) rating 
1
(rdb:130) sort
0
(rdb:130) sort and rating == 1
true
(rdb:130) sort && rating == 1
true
(rdb:130) (sort && rating) == 1
true

2nd, in normal irb, they all return false:

irb(main):001:0> sort = 1
=> 1
irb(main):002:0> rating = 0
=> 0
irb(main):003:0> sort||rating
=> 1
irb(main):004:0> sort and rating
=> 0
irb(main):005:0> sort and rating == 1
=> false
irb(main):006:0> sort && rating == 1
=> false
irb(main):007:0> 0 == true
=> false
irb(main):008:0> 

I know Ruby is so dynamic that…is this situation possible?

  • 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-05T15:51:43+00:00Added an answer on June 5, 2026 at 3:51 pm

    First, in a Boolean context, zero evaluates as true in Ruby, not false. The only things that are false are false itself and nil. Everything else – including empty arrays, empty strings, numerical zeroes – is considered true.

    Second, the Boolean operators might not behave quite the way you think. Since and and && are the same except for precedence, and likewise or and ||. I’ll just refer to them generically as AND and OR. These are the rules:

    a AND b returns a if a is false, b if a is true.

    a OR b returns a if a is true, b if a is false.

    So they are really just shorthand for if a then b else a and if a then a else b, respectively. The truth tables are the familiar ones, but the actual result is always identical with one of the inputs, rather than some newly-constructed pure Boolean value.

    Despite the precedence difference, both versions of this:

    sort AND rating == 1
    

    equate to this:

    sort AND (rating == 1)
    

    So, since sort is zero, which is true, the above expression returns the value of (rating == 1), which is also true (and, in fact, true). Had you gone with rating AND sort == 1, it would be false, because sort is not 1.

    You can see where the other results come from if you follow the rules given above.

    Note: as mentioned by Andrew in the comments, all these various ‘true’ values are still distinct from true itself; 0 may be considered true, but 0 == true is still false. (This applies in any language that allows non-Booleans in a conditional context; for instance, in C, any nonzero number is true, but that doesn’t mean that any two nonzero numbers are equal.)

    Since this can be confusing, especially in verbal or plain text communication where you can’t use formatting tricks like true/true, Rubyists often take a page from Stephen Colbert and refer to the true-but-not-necessarily-true values collectively as “truthy”, and their opposites as “falsy” (or “falsey”; I’ve seen both spellings). This convention is also used in other languages (notably by Douglas Crockford in his JavaScript book) to distinguish the actual Boolean values from other values that evaluate as true or false in conditional contexts.

    ETA: As for your result of sort and rating==1 returning false from irb, I can’t reproduce it. In both 1.8.7 and 1.9.3, I get the expected output based on the above:

    $ irb
    irb(main):001:0> sort=0
    => 0
    irb(main):002:0> rating=1
    => 1
    irb(main):003:0> sort and rating==1
    => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update : I found almost exact similar question , yet it has slightly different
Update: Checking the console, a lot of the users are actually going into the
Update 1: Corrected nonsense code! Thanks for comments, I made a hash of the
Well, that's embarrassing, but I really could not find the answer. We started a
UPDATE `company` SET `itnumber` = '595959' WHERE (id = 932) So the value of
UPDATE: Please see https://softwarerecs.stackexchange.com/questions/71464/java-library-to-insert-invisible-text-into-a-pdf instead. I want to insert invisible text into an existing
UPDATE: I have the answer...I'll post it up for all in a few moments...Thanks
UPDATE 10 Secs later Fixed properly now, and thanks to JF and Gauden. UPDATE
UPDATE: Sorry, forgot to include some of the code (face-palm). I included it in
Update: Is there a way to achieve what I'm trying to do in an

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.