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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:59:37+00:00 2026-05-11T16:59:37+00:00

i have the following class test hash={} def printHash puts hash[1] puts hash[2] puts

  • 0

i have the following

class test
 hash={}

  def printHash 
      puts hash[1]
      puts hash[2]
      puts hash[3]
    end
end

test.new.printHash

this prints:

1 
0 
1

Why does this happen? how can i test whether or not i have put something in that spot of the hash? or am i missing something

  • 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-11T16:59:37+00:00Added an answer on May 11, 2026 at 4:59 pm

    You’re well off the mark, but it appears to be doing something because hash is a builtin function which returns a Fixnum hashcode for the object. When you use square brackets on a Fixnum, you get the value of the specific bit. What you want to do is to create an instance variable, which starts with the sigil @. Also, you have to create instance variables within a method, so we’ll use the one that’s called whenever an object of the class is created, initialize:

    class Test
      def initialize
        @hash = {}
      end
    
      def printHash 
          puts @hash[1]
          puts @hash[2]
          puts @hash[3]
      end
    end
    

    Now you’ll find this prints nil for all three. To test whether a hash has a value for a specific key, you can use has_key?.

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

Sidebar

Related Questions

I have the following ruby code: class Mp def initialize Test.new.mytest Work.new.mywork ha address
I have the following: class Test @@a = 10 def show_a() puts a: #{@@a}
I have the following data: [{class:test,description:o hai,example:a,banana:b}] As this JSON data is already in
I have the following code: class Test(object): _spam = 42 @classmethod def get_spam(cls): cls._spam
I have the following code public class Test { Lock lock = new ReentrantLock();
Suppose you have the following class: class Test : ISerializable { public static Test
Let's say I have the following class: public class Test<E> { public boolean sameClassAs(Object
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have following code public class TEST { public static void main(String arg[]){ try
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;

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.