There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Assignment is fine. When we polled the key, it was given the default value even though it was not added to the hash. This is more obvious if we use numbers.
There are in’s and out’s to every method, so before deciding which is elegant and which is not, be sure you are familiar with all the ramifications of a given method. For instance, Hash#merge will merge two hashes, but any keys that are duplicated will be altered to the have the merged value, not the original one. We would want to sort this out before merging else valuable data could be affected.
hsh[key] = value
is still a valid way to insert an item into the hash. The idea of default value (or nil) is to prevent errors from being thrown if we attempt to poll a key that does not exist.
While you’re investigating #merge, look into #store and see how that fits into to the picture.
That’s reasonable. As you say, there may be a more direct approach and if you look through hash’s methods you’ll find it. (Ctrl+f is your friend here) https://docs.ruby-lang.org/en/2.5.0/Hash.html
When I use defaults for lookup tables it’s because there’s a valid value there which I haven’t set myself.
An example is if you’re counting things, adding 1 each time a thing is found. You’d use 0 as a default.
I don’t agree with using defaults as error handling.
But, obviously, in the interest of trying out how it works, it totally serves its purpose.
@mtf not sure what you mean about duplicate keys being altered. Entries with same keys will be overwritten, but that’s not surprising and no different from if updating them one at a time. (And it is known to be empty so it doesn’t happen anyway)
The only thing to output to the screen for puts nil will be the newline character. A lot of responses from completed code are 'nil', and yes in those instances the literal is what prints. nil is a primitive object with no attributes or methods, meaning no representation, beyond the coerced string we see in the console.
is there any kind of contradiction between what they ask and the result ? They tell to ask for a key that does not exist and then I’m wrong because the key does not exist
Thank you for answering ! I mean I looked at the solution and there is a “puts” as well.
(the first line of code with Hash.new, I added it to try but it didnt work as well without it)