Can I access a variable within a heredoc in Ruby?

丶灬走出姿态 提交于 2019-12-21 06:46:55

问题


If I have a method

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

how can I access the variable p[:name] from within the heredoc?


回答1:


You can interpolate just like in normal strings

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


来源:https://stackoverflow.com/questions/3332849/can-i-access-a-variable-within-a-heredoc-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!