On 13.05.2008 17:46, Paul Dugdale wrote:
> Sorry! I was trying to keep my post simple, but ended up doing a really 
> bad job of explaining what I was trying to achieve...
> 
> I come from a C programming background, so I'm always thinking in terms 
> of pass-by-value and pass-by-reference.
> 
> What I was trying to find out is - is there any way of passing a 
> reference to the value to the 'each' block, rather than a copy of the 
> value.

A reference is indeed passed to the block.  There is no copying of 
values going on.  You can see it because v.downcase! works; this does 
change the object referenced by the Hash.

> I.e. some easy way of doing this:
> hash.each { |k,v| v = my_value }
> 
> Without having to refer to the hash again in the block like this:
> hash.each { |k,v| hash[k] = my_value }

I'm afraid, you'll have to use that idiom.  The only alternative is to 
change your scenario so that it will modify an object, e.g. create an 
intermediary:

irb(main):001:0> Holder = Struct.new :val
=> Holder
irb(main):002:0> h={1=>Holder.new(123)}
=> {1=>#<struct Holder val=123>}
irb(main):003:0> h.each {|k,v| v.val += 10}
=> {1=>#<struct Holder val=133>}
irb(main):004:0>

I doubt though that this is more efficient that using any of

h.keys.each {|k| h[k] += 10}
h.each {|k,v| h[k] = v + 10}

What is the greater context of your issue, i.e. what are you trying to 
achieve?

Kind regards

	robert