Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access a variable within a heredoc in Ruby?

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?

like image 741
ben Avatar asked Jul 26 '10 07:07

ben


People also ask

What is Heredoc in Ruby?

What is heredoc? Heredoc allows us to specify a string as a block of texts where new lines and indentations are maintained. This is useful in Ruby for various cases like specifying multiline text strings, multiline method dynamic definitions and more.

What is string interpolation in Ruby?

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.


1 Answers

You can interpolate just like in normal strings

<<-TERMINATOR   Example #{p[:name]} blah blah blah TERMINATOR 
like image 131
Chubas Avatar answered Sep 27 '22 20:09

Chubas