有人能告诉我这段代码的作用吗?
Could someone tell me what this code does bit for bit?
我能大致理解这段代码的意思。我需要使用它,我也想准确理解它的含义。有人可以逐条向我解释吗?
目前我读作 "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."
我说得对还是漏了一步?我是 Ruby 初学者,正在寻求帮助。
hash = {"flowers" => "blue"}
hash.keys.each { |key| hash[key.to_sym] = hash.delete(key) }
puts hash
如果您查看 .to_sym
的作用,它会转换为符号。我不是专家,但 :a
有点像文字字符串,但有一些不相关的差异这里。
irb(main):008:0> "a".to_sym
=> :a
irb(main):009:0>
你写
"The variable 'hash' includes the key 'flowers' and 'blue'
没有。该散列中只有一对键值对。
代码遍历哈希,在第一次也是唯一一次迭代中,它将键设置为“:flowers”
hash[:flowers]="blah" 将在哈希中创建一个新键,名称为“:flowers”,值为 "blah"
但是代码的作用是在右侧,它确实 hash.delete(key)
让我们看看 hash.delete(key) 做了什么
假设我做 hash={:a=>5, :b=>2} 所以两个键 :a 和 :b,你会看到与每个键关联的值
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>
所以hash.delete(key)删除指定键的键值对,它returns它删除的键对的值。
所以你问题中的代码(特别是如果你查看你看到的结果)它具有重命名所有键的效果,在这种情况下,一个键,因为哈希中只有一个键,并且保持价值。它通过创建新的键值对同时删除原始键值对来实现。它在删除原始键值对之前获取原始键值对并创建新的键值对。
并进一步了解迭代
假设我们用这个散列来做..我将使用多个键值对,因为当有多个键值对时更容易理解散列,因为散列是存储一堆键-值对。
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>
所以是的 hash.keys.each { |key ... }
将遍历所有键
如果你做了 |i,j|你看看会发生什么。
如果您执行 hash.each 并且只是 |i|,那么它会遍历所有键和值,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>
你写
The keys are switched to symbols
您应该使用更专业的术语,例如重命名,但您甚至不需要这样做。你应该能够用代码思考。 hash[key.to_sym]= 应该足够描述了。它获取密钥,从中生成一个符号,然后将一个新值分配给该散列[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."
没有。如果你说 a=b 那么 b
在分配给 a
.
之前首先被评估
在您显示的代码中,在赋值的右侧您有删除,因此首先删除,我们看到它 returns 创建新时使用的原始值键值对。
我能大致理解这段代码的意思。我需要使用它,我也想准确理解它的含义。有人可以逐条向我解释吗?
目前我读作 "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."
我说得对还是漏了一步?我是 Ruby 初学者,正在寻求帮助。
hash = {"flowers" => "blue"}
hash.keys.each { |key| hash[key.to_sym] = hash.delete(key) }
puts hash
如果您查看 .to_sym
的作用,它会转换为符号。我不是专家,但 :a
有点像文字字符串,但有一些不相关的差异这里。
irb(main):008:0> "a".to_sym
=> :a
irb(main):009:0>
你写
"The variable 'hash' includes the key 'flowers' and 'blue'
没有。该散列中只有一对键值对。
代码遍历哈希,在第一次也是唯一一次迭代中,它将键设置为“:flowers”
hash[:flowers]="blah" 将在哈希中创建一个新键,名称为“:flowers”,值为 "blah"
但是代码的作用是在右侧,它确实 hash.delete(key)
让我们看看 hash.delete(key) 做了什么
假设我做 hash={:a=>5, :b=>2} 所以两个键 :a 和 :b,你会看到与每个键关联的值
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>
所以hash.delete(key)删除指定键的键值对,它returns它删除的键对的值。
所以你问题中的代码(特别是如果你查看你看到的结果)它具有重命名所有键的效果,在这种情况下,一个键,因为哈希中只有一个键,并且保持价值。它通过创建新的键值对同时删除原始键值对来实现。它在删除原始键值对之前获取原始键值对并创建新的键值对。
并进一步了解迭代
假设我们用这个散列来做..我将使用多个键值对,因为当有多个键值对时更容易理解散列,因为散列是存储一堆键-值对。
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>
所以是的 hash.keys.each { |key ... }
将遍历所有键
如果你做了 |i,j|你看看会发生什么。
如果您执行 hash.each 并且只是 |i|,那么它会遍历所有键和值,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>
你写
The keys are switched to symbols
您应该使用更专业的术语,例如重命名,但您甚至不需要这样做。你应该能够用代码思考。 hash[key.to_sym]= 应该足够描述了。它获取密钥,从中生成一个符号,然后将一个新值分配给该散列[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."
没有。如果你说 a=b 那么 b
在分配给 a
.
在您显示的代码中,在赋值的右侧您有删除,因此首先删除,我们看到它 returns 创建新时使用的原始值键值对。