Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby HERE-DOC方法参数传递_Ruby_Heredoc - Fatal编程技术网

Ruby HERE-DOC方法参数传递

Ruby HERE-DOC方法参数传递,ruby,heredoc,Ruby,Heredoc,我正在尝试对heredoc使用一个自定义方法,并希望传递参数(这里没有业务案例,我只是尝试学习ruby)。在这种情况下,是否有方法传递参数?这就是我目前所拥有的 方法简单,效果很好 def meth1 self.upcase end str1 = <<MY.meth1 i am a small case string MY # "I AM A SMALL CASE STRING\n" def meth1 自上而下 结束 str1=试着按照 something = "ba

我正在尝试对heredoc使用一个自定义方法,并希望传递参数(这里没有业务案例,我只是尝试学习ruby)。在这种情况下,是否有方法传递参数?这就是我目前所拥有的

方法简单,效果很好

def meth1
  self.upcase
end

str1 = <<MY.meth1
  i am a small case string
MY

# "I AM A SMALL CASE STRING\n"
def meth1
自上而下
结束

str1=试着按照

something = "bananas"

str = <<EOF
  this has some #{something} in!
EOF
something=“香蕉”

str=我猜这就是你想要做的:

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"
def meth2(str1、str2)
str1.upcase+“…”+str2.downcase
结束

str2=meth2(尝试以下方法:

def meth2( item1, item2 )
  item1.upcase + "..." + item2.downcase
end

str2 = meth2 <<EOF1, <<EOF2
 some string
EOF1
 ANOTHER STRING
EOF2

最重要的是,没有办法像您在那里尝试的那样将herdeoc构建到函数中……它们只能以与其他字符串文字相同的位置和方式使用,例如“双引号”或者可以使用“单引号”文本。

我认为您不能将here doc作为参数传递。这样做没有意义。@kevin:我尝试过EOF1,EOF2,但没有这个非常接近。Accepted仍然不是您想要的?有什么不同?
def meth2( item1, item2 )
  item1.upcase + "..." + item2.downcase
end

str2 = meth2 <<EOF1, <<EOF2
 some string
EOF1
 ANOTHER STRING
EOF2
def meth2( item1, item2 )
  item1.upcase + "..." + item2.downcase
end

str2 = meth2 " some string\n", " ANOTHER STRING\n"