我知道连接方法(例如array.join(“”)或array.join(“_”)),它将把数组的元素(或者我相信哈希)放在一起。然而,我从一个问题的解决方案中找到了一个例子,我一直在努力,它看起来像这样:
def find(hash_key)
@options.select { |key,value| key.scan(hash_key).join == hash_key }
end
我完全理解它直到.join == hash_key部分。
有人可以向我解释一下:D
答案 0 :(得分:4)
@options.select {
这将选择@options
...
key.scan(hash_key).join == hash_key
key.scan(hash_key).join
等于hash_key
。
这也可以更明确地写成:
@options.select { |key,value| key.scan(hash_key).join() == hash_key }
(如果join
未提供参数,则假定为""
(空字符串)。
答案 1 :(得分:0)
来自ruby-doc.org:
join(separator=$,) → str
Returns a string created by converting each element of the array to a string, separated by the given separator. If the separator is nil, it uses current $,. If both the separator and $, are nil, it uses empty string.
[ "a", "b", "c" ].join #=> "abc"
[ "a", "b", "c" ].join("-") #=> "a-b-c"
http://zenspider.com/Languages/Ruby/QuickRef.html表示$,
是“打印的输出字段分隔符和数组#join。默认为nil。”。