Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby HERE-DOC method parameter passing

Tags:

heredoc

ruby

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
like image 701
Bala Avatar asked May 30 '13 09:05

Bala


Video Answer


1 Answers

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?

like image 109
sawa Avatar answered Nov 03 '22 00:11

sawa