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

  • Home
  • SEARCH
  • 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 1077043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:31:00+00:00 2026-05-16T21:31:00+00:00

I’m writing convenience methods to check if number is positive or negative like so:

  • 0

I’m writing convenience methods to check if number is positive or negative like so:

class Numeric
  def positive?
    self > 0
  end

  def negative?
    self < 0
  end
end

but in this case I do not know how to handle cases like these:

>> 0.positive?
>> 0.negative?

Update: I’ve updated the typo in the class name. I used numeric because I needed to check the floats as well.

  • 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-16T21:31:01+00:00Added an answer on May 16, 2026 at 9:31 pm

    If the problem is that you’re getting false for both, either you consider 0 to be positive or not. If so, you should have something like:

    def positive?
        self >= 0
    end
    

    If not, leave it as it is, since 0 is neither positive not negative and you should return false for both.

    However if the problem is that you’re getting errors with 0.positive? (far more likely), the reason you’re getting a problem is because 0 is a FixNum, not a Number. You can see that with the following message:

    testprog.rb:12: undefined method `positive?' for 0:Fixnum (NoMethodError)
    

    You should probably add it to Fixnum itself, or Integer, or Numeric, the base class for various numeric types like FixNum and BigNum. Where you inject your convenience methods depends on how widely you want them available.

    For example, if you change your code to the following (I’m including test code here):

    class Numeric
        def positive?
            self > 0
        end
    
        def negative?
            self < 0
        end
    end
    
    print " 0 positive?: ",  0.positive?,"\n"
    print " 0 negative?: ",  0.negative?,"\n"
    print " 0 zero?    : ",  0.zero?,"\n\n"
    
    print "99 positive?: ", 99.positive?,"\n"
    print "99 negative?: ", 99.negative?,"\n"
    print "99 zero?    : ", 99.zero?,"\n\n"
    
    print "-2 positive?: ", -2.positive?,"\n"
    print "-2 negative?: ", -2.negative?,"\n"
    print "-2 zero?    : ", -2.zero?,"\n\n"
    

    it then works fine, outputting:

     0 positive?: false
     0 negative?: false
     0 zero?    : true
    
    99 positive?: true
    99 negative?: false
    99 zero?    : false
    
    -2 positive?: false
    -2 negative?: true
    -2 zero?    : false
    

    as expected.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

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.