New Ruby Question: Hashes

i has 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 is this happening? how can i check if i put something in this hash place? or am i missing something

+3
source share
2 answers

, , , - , hash , Fixnum hashcode . Fixnum, . , sigil @. , , , , , initialize:

class Test
  def initialize
    @hash = {}
  end

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

nil . , , has_key?.

+10

"" , , printHash, , nil ( ), Pesto , "hash" - - .

"@" initialize ( "new" ), .

0

Source: https://habr.com/ru/post/1708088/


All Articles