* in { |key, value| value * 2 }

14 views
Skip to first unread message

dreamer

unread,
Jul 8, 2019, 11:24:57 AM7/8/19
to

Hi all,
I'm new to ruby and trying to understand this bit of code:

hash.update(hash) { |key, value| value * 2}

In particular what is the meaning of the value * 2?

When I tried using IRB to multiply values in a hash, either
I received an error or it multiplied the values and gave me
the product.

Any insights would be greatly appreciated. Thank you.


--
--
danny lee <dre...@panix.com> (fka dan...@mindvox.com, dre...@escape.com)

"I remarked to Dennis [Ritchie] that easily half the code I was writing in
Multics was error recovery code. He said, "We left all that stuff out [of
Unix]. If there's an error, we have this routine called panic, and when it
is called, the machine crashes, and you holler down the hall, 'Hey, reboot
it.'"- Tom Van Vleck.

Doc O'Leary

unread,
Jul 14, 2019, 11:45:02 AM7/14/19
to
For your reference, records indicate that
dreamer <dre...@panix.com> wrote:

> hash.update(hash) { |key, value| value * 2}
>
> In particular what is the meaning of the value * 2?

It means the same thing it would mean anywhere else: multiply the contents
of the “value” variable by 2. Is it really blocks that have you confused,
or the use of the update method? For newer versions of Ruby, the other
choice to do the same operation would be the transform_values! method.

> When I tried using IRB to multiply values in a hash, either
> I received an error or it multiplied the values and gave me
> the product.

You show no examples, errors or otherwise. The line you posted should do
what I *think* you want, but you many need to explain your intent more
completely.

--
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly


dreamer

unread,
Jul 16, 2019, 11:27:48 AM7/16/19
to
Sorry, that was bad netiquette on my part. I sometimes get focused on my
own thoughts and understanding and don't speak (or write) in a way that fully
explains the situation to an outside observer.

Here is an example I found on stackoverflow (https://stackoverflow.com/questions/5215713/ruby-what-is-the-easiest-method-to-update-hash-values)

h = { 1 => 10, 2 => 20, 5 => 70, 8 =>90, 4 => 34 }

which the writer would like to change to:

h = { 1 => foo(10), 2 => foo(20), 5 => foo(70), 8 => foo(90), 4 => foo(34) }

and the "best" answer was the line above:

hash.update(hash) { |key, value| value * 2 }

My confusion was really about the "value * 2", the answer's author wrote:

"Note that we're effectively merging hash with itself. This is
needed because Ruby will call the block to resolve the merge
for any keys that collide, setting the value with the return
value of the block."

I was wondering why this would be better/different than just doing an
update to the hash and key. And what is meant by 'resolving the merge
for any keys that collide'.


I was confused about blocks AND how to use the update method, but
this bit had me doubly confused.

Now that I've had time to step and back and think about it, I may just
be overthinking and this bit of code could be meaningless, in a sense.





--
--
danny lee <dre...@panix.com>

"Every artist was first an amateur." - Ralph Waldo Emerson

Doc O'Leary

unread,
Jul 16, 2019, 3:44:26 PM7/16/19
to
For your reference, records indicate that
dreamer <dre...@panix.com> wrote:

> Here is an example I found on stackoverflow (https://stackoverflow.com/questions/5215713/ruby-what-is-the-easiest-method-to-update-hash-values)
>
> h = { 1 => 10, 2 => 20, 5 => 70, 8 =>90, 4 => 34 }
>
> which the writer would like to change to:
>
> h = { 1 => foo(10), 2 => foo(20), 5 => foo(70), 8 => foo(90), 4 => foo(34) }
>
> and the "best" answer was the line above:
>
> hash.update(hash) { |key, value| value * 2 }
>
> My confusion was really about the "value * 2"

Well, in that case the answer did confuse the issue a bit by giving that
instead of “foo(value)” to correspond with the question.

> "Note that we're effectively merging hash with itself. This is
> needed because Ruby will call the block to resolve the merge
> for any keys that collide, setting the value with the return
> value of the block."
>
> I was wondering why this would be better/different than just doing an
> update to the hash and key.

By doing what instead? You certain *could* just iterate through using an
enumerator, making assignments explicitly. But using a block with the
update method is a much more convenient way to accomplish the task (and
generally why so many generic Enumerable methods accept blocks).

> And what is meant by 'resolving the merge
> for any keys that collide'.

That’s just the way the update method works. Its “normal” use is to merge
two different hashes, and the block is only run to resolve the case where
there are keys that exist in both. But used against itself, *all* entries
will match, and thus be changed by the code in the given block.

dreamer

unread,
Jul 17, 2019, 7:21:54 PM7/17/19
to
Doc O'Leary <drol...@2017usenet1.subsume.com> wrote:
Thank you for the clarification. I appreciate you taking the time to
respond and give me a better understanding of this. I'm finding Ruby
intuitive and, may I say, "fun" :)




.------------------( Danny Lee <dre...@panix.com> )------------------.
I have been impressed with the urgency of doing. Knowing is not enough;
we must apply. Being willing is not enough; we must do.
~ Leonardo da Vinci
Reply all
Reply to author
Forward
0 new messages