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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:07:20+00:00 2026-05-14T05:07:20+00:00

I’m starting with Ruby, and while making some test samples, I’ve stumbled against an

  • 0

I’m starting with Ruby, and while making some test samples, I’ve stumbled against an error in the code that I don’t understand why it happens.

The code pretends to tests if a variable finn is defined?() and if it is defined, then it increments it. If it isn’t defined, then it will define it with value 0 (zero).
As the code threw an error, I started to decompose it in small pieces and run it, to better trace where the error was comming from.
The code was run in IRB irb 0.9.5(05/04/13), using ruby 1.9.1p378

First I certify that the variable finn is not yet defined, and all is ok:

?> finn
NameError: undefined local variable or method `finn' for main:Object
        from (irb):134
        from /home/paulo/.rvm/rubies/ruby-1.9.1-p378/bin/irb:15:in `<main>'
>>

Then I certify that the following inline-condition executes as expected, and all is ok:

?> ((defined?(finn)) ? (finn+1):(0))
=> 0

And now comes the code that throws the error:

?> finn=((defined?(finn)) ? (finn+1):(0))
NoMethodError: undefined method `+' for nil:NilClass
        from (irb):143
        from /home/paulo/.rvm/rubies/ruby-1.9.1-p378/bin/irb:15:in `<main>'

I was expecting that the code would not throw any error, and that after executing the variable finn would be defined with a first value of 0 (zero). But instead, the code thows the error, and finn get defined but with a value of nil.

>> finn
=> nil

Where might the error come from?!? Why does the inline-condition work alone, but not when used for the finn assignment?

Any help apreciated 🙂

  • 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-14T05:07:21+00:00Added an answer on May 14, 2026 at 5:07 am

    In this line,

    finn=((defined?(finn)) ? (finn+1):(0))
    

    When you write finn=, it declares finn, and sets it to nil until the assignment occurs. Therefore, defined?(finn) returns true , so it attempts to execute finn+1, which is equivalent to nil+1, which results in the NoMethodError you mentioned above.

    You may be looking for this:

    finn ||= 0
    finn += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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

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.