+ 3

Hash in Ruby and dictionary in Python are the same things?

For example if we have dictionary in python: d = {"test": 1, 2: 2} and call d[2] that return: 2, but when we call d[3] that return KeyError: 3. In Ruby if we have hash: h = {"test" =>1, 2 => 2} and try to call h[3] that not return anything even error is not happend. Why there isn't error in the second case?

26th Jan 2019, 10:29 AM
Kaloyan Kostov
Kaloyan Kostov - avatar
2 odpowiedzi
+ 3
In other languages they are called Dictionaries (as in Python), in Ruby they are hashes, in the hashes when a key is called that does not exist or has no value, they take the value of nil (if there is no value that co-exists or the value does not exist ) Example: hash.key (value) returns the key for the value given in the hash, nil (if there is no value that matches or the value does not exist) It is for this reason that in Ruby you do not get an error like in Python, since the value does not exist, it is simply ignored.
5th Feb 2019, 10:53 PM
XC0D3
XC0D3 - avatar
0
bro please how can i recode a python script to output the same input but with different code than the one used to write it
29th Apr 2019, 3:25 PM
Tygaking