Why do assignment statements return a value?

To my understanding, assignment s = “Hello”; should only cause “Hello” to be assigned to s, but the operation shouldn’t return any value. Your understanding is 100% incorrect. Can you explain why you believe this false thing? What is the reasoning behind allowing assignment statements to return a value? First off, assignment statements do not …

Read more

Automatic counter in Ruby for each?

As people have said, you can use each_with_index but if you want indices with an iterator different to “each” (for example, if you want to map with an index or something like that) you can concatenate enumerators with the each_with_index method, or simply use with_index: blahs.each_with_index.map { |blah, index| something(blah, index)} blahs.map.with_index { |blah, index| …

Read more

How to change Hash values?

my_hash.each { |k, v| my_hash[k] = v.upcase } or, if you’d prefer to do it non-destructively, and return a new hash instead of modifying my_hash: a_new_hash = my_hash.inject({}) { |h, (k, v)| h[k] = v.upcase; h } This last version has the added benefit that you could transform the keys too.