I can understand largely what this bit of code means. I need to use it and I also want to understand what it means precisely. Could someone explain it to me piece by piece?
Currently I read it as "The variable 'hash' includes the key 'flowers' and 'blue'." "Each iterates over the hash and only its keys with the placeholder 'keys'. The keys are switched to symbols and inserted into the hash via the []'s after hash is reassigned its value by deleting all of the previous keys with the .delete method."
Am I right or am I missing a step? I am a Ruby beginner and am looking for some help.
hash = {"flowers" => "blue"}
hash.keys.each { |key| hash[key.to_sym] = hash.delete(key) }
puts hash
if you look up what .to_sym
does, it converts to a symbol.. i'm no expert but :a
is a bit like a literal string with some differences that aren't relevant here.
irb(main):008:0> "a".to_sym
=> :a
irb(main):009:0>
You write
"The variable 'hash' includes the key 'flowers' and 'blue'
No. There is only one key-value pair in that hash.
The code iterates through the hash, on the first and only iteration, it sets key to ":flowers"
hash[:flowers]="blah" will create a new key in the hash, with name ":flowers" and value "blah"
But what that code does, is on the right hand side, it does hash.delete(key)
Let's see what hash.delete(key) does
say I do hash={:a=>5, :b=>2} so two keys :a and :b, and you see the value associated with each key
irb(main):009:0> hash
=> {:a=>4, :b=>2}
irb(main):010:0> hash.delete(:a)
=> 4
irb(main):011:0> hash
=> {:b=>2}
irb(main):012:0>
So hash.delete(key) deletes the key-value pair at the specified key, and it returns the value of the key-pair that it deleted.
So that code in your question (particularly if you look at the result you see) it has the effect of renaming all the keys, in this case, one key, as there is only one key in the hash, and keeping the value. It does this by creating new key-value pairs while also deleting the original ones It gets the value of the original one before it deletes it and creates the new key value pair.
And to look more at the iteration
Say we do it with this hash.. and i'll use more than one key value pair as it's easier to understand a hash when there are multiple key value pairs, since a hash is to store a bunch of key-value pairs.
irb(main):026:0> hash
=> {"a"=>2, "b"=>4}
irb(main):027:0> hash.keys.each { |key| puts key }
a
b
=> ["a", "b"]
irb(main):028:0> hash.each { |i,j| puts i }
a
b
=> {"a"=>2, "b"=>4}
irb(main):029:0>
So yeah hash.keys.each { |key ... }
is going to iterate through all the keys
If you did |i,j| you see what happens.
If you do hash.each and just |i|, then it iterates through all keys and values, key1 value1 key2 value2 e.t.c.
irb(main):029:0> hash.each { |i| puts i }
a
2
b
4
=> {"a"=>2, "b"=>4}
irb(main):030:0>
You write
The keys are switched to symbols
You should be using a more technical term, like rename, but you don't even need to do that. You should be able to think in code. hash[key.to_sym]= should be descriptive enough. It takes the key, produces a symbol from it, then will assign a new value to that hash[newkey]
and inserted into the hash via the []'s after hash is reassigned its value by deleting all of the previous keys with the .delete method."
No. If you say a=b then b
is evaluated first before it's assigned to a
.
In the code you show, on the right hand side of the assignment you have the deletion, so the deletion is done first, and we see it returns the original value which is used when creating the new key-value pair.