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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:09:23+00:00 2026-06-17T17:09:23+00:00

I want to do this lol = Klass.new(values) unless lol print false end lol.other_method

  • 0

I want to do this

lol = Klass.new(values)

unless lol 
   print "false"
end

lol.other_method  # it is not nil or false, it is a Klass instance!

But lol, in this case, is not nil or false, but an object who can act as a false based on some internal value. I have this alternative

lol = Klass.new(values)

unless lol.to_bool
   print "false"
end

But it is ugly IMHO.

I was thinking in extend the FalseClass or play with == but without success. Any Ideas?

  • 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-17T17:09:24+00:00Added an answer on June 17, 2026 at 5:09 pm

    Unfortunately, this is not possible.

    This is one of those annoying cases where Ruby is not object-oriented. In OO, it must be possible for one object to simulate another (in fact, depending on whom you ask, this is the very definition of OO – remember that OO came out of simulation), but it is not possible to build an object which simulates false.

    This is because, in Ruby, conditional control structures are baked into the language and don’t translate into message sends, whereas in other OO languages they are just regular message sends (or at least translate into message sends, just like for in Ruby translates into each). For example, in Smalltalk, Booleans are actually implemented using the Church encoding of Booleans you know from Lambda Calculus, and translated to Ruby they look a bit like this:

    class FalseClass
      def if(&block)
        # do nothing
      end
    
      def if_then_else(then_lambda, else_lambda)
        else_lambda.()
      end
    
      def not
        true
      end
    
      def and(&block)
        self
      end
    
      def or(&block)
        block.()
      end
    end
    

    And TrueClass is just the mirror image:

    class TrueClass
      def if(&block)
        block.()
      end
    
      def if_then_else(then_lambda, else_lambda)
        then_lambda.()
      end
    
      def not
        false
      end
    
      def and(&block)
        block.()
      end
    
      def or(&block)
        self
      end
    end
    

    And then, instead of something like

    if 2 < 3 then foo end
    if 2 < 3 then bar else baz end
    

    You would have

    (2 < 3).if { foo }
    (2 < 3).if_then_else(-> { bar }, -> { baz })
    
    # using the new keyword arguments in Ruby 2.0, it almost reads like Smalltalk:
    class FalseClass
      def if(then: -> {}, else: -> {})
        else.()
      end
    end
    
    class TrueClass
      def if(then: -> {}, else: -> {})
        then.()
      end
    end
    
    (2 < 3).if(then: -> { bar }, else: { baz })
    

    That way, you can easily create an object which simulates false simply by implementing the respective methods.

    In other cases, where some object really absolutely must be an instance of a specific class and not just speak the correct protocol, Ruby provides an escape hatch. For example, if a method really requires an Array as an argument, then it will first try to call to_ary to at least give you a chance to convert your object into an Array. The same goes for to_str, to_int, to_proc, to_float etc. But there is no equivalent to_bool protocol.

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

Sidebar

Related Questions

I want this highchart - http://jsfiddle.net/zPDca/ inside a popup. But if i decrease its
I want this text all on one line, not spaced on two sepearate lines.
I want this to be on top... how can I do this and not
I only want this function to run if .toolbar li does not have the
I know C# and I want to convert this code into VB.NET. lol.Navigated +=
a file content just like this: ##this is the comment but this is not
Given: myChart = new ganttChart(chart1); function ganttChart(gContainerID) { this.variable1 = lol; this.variable2 = hai
I want to convert a string like this: asd foo bar ( lol bla
this is a question similar to this one . But I am not good
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to

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.