15. More Methods, more Solutions

Hey community,

in fact I already cleared Number 15, but there is one point my brain seems to be missing. The text says:

my_hash = { one: 1, two: 2, three: 3 }

my_hash.each_key { |k| print k, " " }
# ==> one two three

my_hash.each_value { |v| print v, " " }
# ==> 1 2 3

The first one is going after the string, the second one is going after the integer, so far, so clear.

But how could I program only one, so that it is going after the integer right away without caring about the string? I think I’ve learned it already here, but somehow I made a break for a while, but that knowledge would be important to me. An example would help.


Hi @piurry,

Are you referring to .each?

my_hash = { one: 1, two: 2, three: 3 }
my_hash.each { |k, v| print k, " ", v, "\n" }

Run it here

1 Like

So easy…that is exactly what i wanted - Thank you @gaurangtandon

1 Like

2 posts were split to a new topic: 15. More Methods, More Solutions