Using Hash.new 7/6


#1

i am stock in here
this is my code i dont what to do can someone help please.
Hash.new => pets.Hash


#2

Here is the example:

You can also create a hash using Hash.new, like so:
my_hash = Hash.new

And here are the instructions:

Use Hash.new to create a new hash called pets.

In your opinion, what should be replaced from the example to match the instructions?


#3

I have the same problem. I tried following your advice, and put in this code:
my_hash = Hash.new
my_hash[1] = "pets"
puts my_hash[1]
However, it keeps saying to declare the variable, "pets"? Can you please help me?


#4

Probably because you’re testing it in an exercise that requires a variable called pets?

Your code works in the lab, test it if you want:

labs repl.it


#5

Try this
pets = Hash.new { [] }


#6

Thanks, the code you gave me had succeeded in the exercise.


#7

how did you figure out such combination? amazing...I've tried lots of word arrays but never got it right. Could you please explain the meaning of last two brackets?


#8

To be honest, it was a accident. I think that exercise is bugged.

I do believe i know how i got it to work though. if you put {} instead of Hash.new it does the same thing. And i believe the [] does nothing in that code.

And it worked so i didn't really bother to research it more. :smile:


#9

Yeah, I tried to delete the last two brackets and it also succeeded. Maybe the point here is the word sequence, which is the opposite of the normal logic: "pets" is in front of "Hash.new". I still have the question that what's the difference between my_hash and Hash.new? Only the sequence of words when you using them?


#10

I did some more research but it's hard to find info about it... But i did find some info that might be helpful.

As you know, a hash is an unordered list of unique keys and their values, like this "options = { font_size: 10, font_family: "Arial" }"

I did some more research but it's hard to find info about it...

But i did find some info that might be helpful.

As you know, a hash is an unordered list of unique keys and their values, like this "options = { font_size: 10, font_family: "Arial" }"

Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used.

my_hash is a example i think, it's like putting "options" like in the hash above.

Hope its helpful


#11

I think you are right. Still on my way conquering Ruby and wish myself good luck. Thank you for the help, it is very helpful!:smile:


#12

I had some trouble with this for a while, but here's what I ended up doing:

pets = Hash.new

^ (edit: turns out that's all you need to move on with the lesson)

pets = {
    "pet1" => "cat"
}

#13

try this: pets=Hash.new
i guess this is what the exercise requires


#14

Yeah! This works in the exercise. I thought they wanted us to put in a value "pets"


#15

I did this:

my_hash = Hash.new
Hash = pets

For some reason, once I removed the ".new" it allowed me to set the variable to pets.


#16

I just go with

pets = Hash.new

and succeed


#17

I just did the following on Codeacademy, and it worked

pets = Hash.new
pets = {
pet1 => cat,
pet2 => dog,
pet3 => lizard
}


#18

you need to put something in the hash. try this:
pets = Hash.new
pets["cat_name"] = "cat"
this gives cat_name a value of cat in the hash pets


#19

that works because you have created a hash but codecademy does not check how