FAQ: Data Structures - Hashes

This community-built FAQ covers the “Hashes” exercise from the lesson “Data Structures”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Hashes

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 (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 (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi everyone!

When I puts the value for “moka”, why does it omit the zero when suing the the literal notation code?

Thanks

In the first version, you are assigning numbers as the values for the keys.
In the second version, you are assigning strings as the values.

// You assigned a string value
my_hash["moka"] = "2.80"
puts my_hash["moka"] // 2.80

// Assigning a number value
my_hash["moka"] = 2.80
puts my_hash["moka"] // 2.8 

Thanks for your reply mtrtmk.

Does that mean that if a scenario arose where I needed to puts the zero, I would have to use the string version “2.80” and not the number version 2.80?

Thanks

You can format the output of print/puts:

// Number assigned to x
x = 2.80

puts x
// Output: 2.8

puts "%.2f" % x
// Output: 2.80

// String interpolation
puts "The number to two decimal places is #{"%.2f" % x} !!!"
// Output: The number to two decimal places is 2.80 !!!

Thanks mtrtmk. I think that’s a bit advanced for my feeble brain right now :sweat_smile: