I am trying to use a custom method with here-doc and want to pass parameter (there is no business case, I am merely trying to learn ruby). Is there a way to pass parameter in this case? This is what I have so far.
Simple method, just works fine.
def meth1
  self.upcase
end
str1 = <<MY.meth1
  i am a small case string
MY
# "I AM A SMALL CASE STRING\n"
Now, I thought let us drop some parameters and tried different variations and irb gives me a blank stare.
#variation 1
def meth2( <<EOF1, <<EOF2 )
  EOF1.upcase + "..." + EOF2.downcase
end
str2 = <<MY.meth2
 some string
EOF1
 ANOTHER STRING
EOF2
MY
                My guess is that this is what you are trying to do:
def meth2(str1, str2)
  str1.upcase + "..." + str2.downcase
end
str2 = meth2(<<EOF1, <<EOF2)
 some string
EOF1
 ANOTHER STRING
EOF2
str2 # => " SOME STRING\n... another string\n"
If you don't intent to indent, see here. ← See my play with words here?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With