【问题标题】:Can I access a variable within a heredoc in Ruby?我可以在 Ruby 的 heredoc 中访问变量吗?
【发布时间】:2011-03-20 22:58:58
【问题描述】:

如果我有方法

  def some_method p = {}
    string = <<-MY_TERMINATOR
      Example text blah blah
      lorem ipsum something or another
    MY_TERMINATOR
  end

如何从 heredoc 中访问变量 p[:name]?

【问题讨论】:

    标签: ruby string heredoc


    【解决方案1】:

    您可以像在普通字符串中一样进行插值

    <<-TERMINATOR
      Example #{p[:name]} blah blah blah
    TERMINATOR
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-06
      • 2017-11-11
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多