Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!not-for-mail From: dreamer Newsgroups: comp.lang.ruby Subject: Re: * in { |key, value| value * 2 } Date: Tue, 16 Jul 2019 15:27:42 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 66 Message-ID: References: NNTP-Posting-Host: panix3.panix.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reader1.panix.com 1563290862 14503 166.84.1.3 (16 Jul 2019 15:27:42 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Tue, 16 Jul 2019 15:27:42 +0000 (UTC) User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (NetBSD/7.1.2 (amd64)) Xref: csiph.com comp.lang.ruby:7410 Doc O'Leary wrote: > For your reference, records indicate that > dreamer 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. > 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 "Every artist was first an amateur." - Ralph Waldo Emerson