转义散列中的 ruby 值

escaping ruby values in hash

我在声明 ruby 哈希时尝试使用其他变量的值。正如我预期的那样,这些值现在正在被转义。我该如何解决这个问题?

变量

ipa_url、名称、版本和包标识符

代码

data = {
            plist: {
              dict: {
                key: 'items',
                array: {
                  dict: {
                    key: %w('assets','metadata'),
                    array: {
                      dict: [{ key:    %w('kind','url'),
                               string: %w('software-package',
                                          "#{ipa_url") },
                             { key:    %w('kind','url'),
                               string: %w('display-image',"#{icon_url.to_s}") },
                             { key:    %w('kind','url'),
                               string: %w('full-size-image',
                                          "#{icon_url}") }],
                      dict: { key: %w('bundle-identifier','bundle-version',
                                      'kind','title'),
                              string: %w("#{bundle-identifier}","#{version}",
                                         'software',"#{name}")
                      }
                    }
                  }
                }
              }
            }
          }

zenspider 详细介绍了这一点,但出于 SO 目的,这里是细分:

%q(无插值)

[6] pry(main)> hey
=> "hello"
[7] pry(main)> hash = { 'hi' => %q("#{hey}", 'how are you') }
=> {"hi"=>"\"\#{hey}\", 'how are you'"}

%Q(插值和反斜杠)

[8] pry(main)> hash = { 'hi' => %Q("#{hey}", 'how are you') }
=> {"hi"=>"\"hello\", 'how are you'"}

%(插值和反斜杠)

[9] pry(main)> hash = { 'hi' => %("#{hey}", 'how are you') }
=> {"hi"=>"\"hello\", 'how are you'"}

%W(插值) 如 Uri 所示:

[7] pry(main)> hash = { 'hi' => %W(#{hey} how are you) }
=> {"hi"=>["hello", "how", "are", "you"]}

%w 标识符用于从 space 分隔文本创建数组:

%w(this is a test)
# => ["this", "is", "a", "test"]

如果你想在那里使用字符串插值,你应该使用 %W 代替:

variable = 'test'
%W(this is a #{variable})
# => ["this", "is", "a", "test"]