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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:24:18+00:00 2026-05-13T20:24:18+00:00

What is happening here?? irb(main):001:0> a= nil => nil irb(main):002:0> b = nil =>

  • 0

What is happening here??

irb(main):001:0> a= nil
=> nil
irb(main):002:0> b = nil
=> nil
irb(main):003:0> a
=> nil
irb(main):004:0> a+b
NoMethodError: undefined method `+' for nil:NilClass
    from (irb):4
    from :0
irb(main):005:0> if a.nil? or b.nil?; a,b=0;end;
irb(main):006:0* c = a+b
TypeError: nil can't be coerced into Fixnum
    from (irb):6:in `+'
    from (irb):6
    from :0
irb(main):007:0>

How can you safely perform arithmetic by transforming nil to an number?

  • 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-13T20:24:18+00:00Added an answer on May 13, 2026 at 8:24 pm

    Why would you want to add nil? It’s specifically designed to be something that represents the lack of a value (note: nil is NOT 0).

    If what you’re actually wanting to do is set both values to 0 if either is currently nil, then what you actually want is this:

    if a.nil? or b.nil?; a,b=0,0; end
    

    Your mistake in the code above was the a,b=0 portion, which only sets the value of a to 0 – it sets b to nil because the left hand side is looking for two values, and only one is provided on the right (so the others are assumed to be nil).

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

Sidebar

Related Questions

I am getting very strange outputs from my IRB console. Here it is: irb(main):001:0>
(LocalVariable)ABC.string(Name) = (IDataReader)dataReader.GetString(0); This name value is coming from database. What happening here is
Here's a little copy/paste from fxri. Why is this happening and how do I
I don't understand what's happening here, I've copied this code from another project (which
I need some help understanding what's happening here. This code is from a models/log.py
I don't understand what's happening here. If someone could explain, I would appreciate it.
I can't capture what is happening here. My global.asax file has nothing exotic in
Can anyone tell me what is happening here? <?php // true var_dump('\\ ' ===
I don't quite understand whats happening here a=[1,2,3,4] b=[5,6,7,8] a[len(a):] = b seems to
I've been trying to figure out what exactly is happening here. I'm just trying

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.