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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:26:18+00:00 2026-05-12T17:26:18+00:00

Pardon the total newbiew question but why is @game_score always nil? #bowling.rb class Bowling

  • 0

Pardon the total newbiew question but why is @game_score always nil?

#bowling.rb

class Bowling
  @game_score = 0
    def hit(pins)
        @game_score = @game_score + pins
    end

    def score
        @game_score
    end
end
  • 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-12T17:26:18+00:00Added an answer on May 12, 2026 at 5:26 pm

    Let’s walk through the code, shall we?

    #bowling.rb
    
    class Bowling
      @game_score = 0 # (1)
    

    At this point (1), we are still inside the class Bowling. Remember: classes are just objects like any other. So, at this point you are assigning 0 to the instance variable @game_score of the class object Bowling.

     def hit(pins)
      @game_score = @game_score + pins # (2)
    

    Now (2), we are inside an instance method of the Bowling class. I.e.: this is a method that is going to belong to an instance of Bowling. So, now the instance variable @game_score belongs to an instance of the Bowling class, and not to the class itself.

    Since this instance variable is never initialized to anything, it will evaluate to nil (in Ruby, uninitialized variables always evaluate to nil), so this evaluates to @game_score = nil + pins and since nil doesn’t have a #+ method, this will result in a NoMethodError exception being raised.

     end
     def score
      @game_score # (3)
    

    And here (3), we are again inside an instance method of the Bowling class. This will always evaluate to nil, for the reason I outlined above: @game_score is never initialized, therefore it evaluates to nil.

     end
    end
    

    We can use Ruby’s reflection capabilities to take a look at what’s going on:

    p Bowling.instance_variable_get(:@game_score) # => 0
    b = Bowling.new
    p b.instance_variable_get(:@game_score) # => nil
    

    Now let’s inject a value into the instance variable:

    b.instance_variable_set(:@game_score, 1)
    p b.score # => 1
    b.hit(3)
    p b.score # => 4
    

    So, we see that everything works as it should, we only need to figure out how to make sure the instance variable gets initialized.

    To do that, we need to write an initializer method. Strangely, the initializer method is actually a private instance method called initialize. (The reason why initialize is an instance method and not a class method, is actually quite simple. Ruby splits object creation in two phases: memory allocation and object initialization. Memory allocation is done by a class method called alloc and object initialization is done by an instance method called initialize. (Objective-C programmers will recognize this.) The reason why alloc is a class method is simply that at this point in the execution there is no instance yet. And the reason that initialize is an instance method is that object initialization is obviously per-object. As a convenience, there is a standard factory class method called new that calls both alloc and initialize for you.)

    class Bowling
     def initialize
      @game_score = 0
     end
    end
    

    Let’s test this:

    c = Bowling.new
    p c.score # => 0
    c.hit(2)
    p c.score # => 2
    

    BTW: just some minor Ruby style tips: indentation is 2 spaces, not 1 tab. And your hit method would more idiomatically be @game_score += pins.

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

Sidebar

Related Questions

Pardon me if my question looks stupid but I am totally new to Workflow.
Pardon me for this long story, but I think the question merits it. I
Pardon the vague question, but I've just inherited a project to build a couple
Pardon me, this really is a noob question but please understand that I do
Please pardon me asking this rudimentary question, but let me know if anyone knows
Pardon me if there's a similar question somewhere on here already, but I couldn't
Pardon me if this is a n00b-ish question but if the shoe fits I'll
Please pardon me for a n00bish question. Please consider the following code: public class
Well, pardon for asking such a question. But searching didn't help me. I did:
Pardon me for asking a mundane newbie question but I seem to be stuck

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.