+ 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?
2 Antworten
+ 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.
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