+ 2

What is the output of this code? And How?

hash = Hash.new() hash[9] = "a" hash[8] = "b" hash[7] = "c" hash[6] = "d" print hash.count () hash.delete[9] print hash.count()

11th Sep 2017, 3:11 PM
RajX Basu
RajX Basu - avatar
2 Antworten
+ 2
in Ruby everything is an object so first hash = Hash.new()//this is used to create hash hash[9] = "a"//first value in hash hash[8] = "b"//2nd value in hash hash[7] = "c"//3rd value in hash hash[6] = "d"//4th value in hash print hash.count ()//it will count the value in hash which is 4 hash.delete[9]//it will delete the value" b" print hash.count()//now count become 3 so output is 43
11th Sep 2017, 3:56 PM
MsJ
MsJ - avatar
+ 7
Remove space in line 6: print hash.count() Change braces in line 10: hash.delete(9) Then you'll get the output you expected ;)
11th Sep 2017, 3:47 PM
Tashi N
Tashi N - avatar