gpt4 book ai didi

ruby - Ruby 中的 %w{} 和 %W{} 大写和小写百分比 W 数组文字有什么区别?

转载 作者:数据小太阳 更新时间:2023-10-29 06:21:50 26 4
gpt4 key购买 nike

%w[ ]   Non-interpolated Array of words, separated by whitespace
%W[ ] Interpolated Array of words, separated by whitespace

用法:

p %w{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %W{one one two three 0 1 1 2 3} # = > ["one", "one", "two", "three", "0", "1", "1", "2", "3"]
p %w{C:\ C:\Windows} # => ["C: C:\\Windows"]
p %W{C:\ C:\Windows} # => ["C: C:Windows"]

我的问题是……有什么区别?

最佳答案

%W 将字符串视为双引号,而 %w 将它们视为单引号(因此不会插入表达式或大量转义序列)。用 ruby​​ 表达式再次尝试你的数组,你会发现不同。

示例:

myvar = 'one'
p %w{#{myvar} two three 1 2 3} # => ["\#{myvar}", "two", "three", "1", "2", "3"]
p %W{#{myvar} two three 1 2 3} # => ["one", "two", "three", "1", "2", "3"]

关于ruby - Ruby 中的 %w{} 和 %W{} 大写和小写百分比 W 数组文字有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5751831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com