ruby 这里的文档 [英] ruby here documents

查看:51
本文介绍了ruby 这里的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Ruby 编写一个方法,该方法使用带有输入变量的 HTML 代码的 here-document 并相应地填充它们.

I am trying to write a method in Ruby that uses a here-document of HTML code with input variables and fills them in accordingly.

我的方法是:

calcForm(left, op, right, result)

我使用的 html 标签是

The html tags I am using are

<input type="text" name="left" value="?????"> 
<select name="op">
<option value="add" ?????>+</option>
<option value="mul" ?????>*</option> 
</select>
<input type="text" name="right" value="?????"> 
=
?????

到处都是问号,我的方法必须用变量 left、op、right 和 result 填充.

Everywhere there are question marks my method has to fill in with variables left, op, right, and result.

例如,

calcForm(6, "mul", 7, 42) 

应该返回字符串:

<input type="text" name="left" value="**6**"> 
<select name="op">
<option value="add">+</option>
<option value="mul" **selected**>*</option> 
</select>
<input type="text" name="right" value="**7**"> 
=
**42**

所以,根据op的值,在add"或jul"之后应该出现selected"这个词,left和right的值应该填入value="...",结果应该是出现在最后一行.

So, the word "selected" should appear after "add" or "jul" depending on the value of op, the values of left and right should be filled in in value = "...", and the result should appear on the last line.

我是 ruby​​ 的新手,但这是我迄今为止对这里文档的了解所做的:

I am new to ruby but this is what I have done so far with my knowledge of here docs:

the_tags = <<HERE
<input type="text" name="left" value=#{left}> 
<select name="op">
<option value="add" #{op}>+</option>
<option value="mul" #{op}>*</option> 
</select>
<input type="text" name="right" value=#{right}> 
=
#{result}
HERE

def calcForm(left,op,right,result)

我被困在这一点上.我对如何将我的方法 calcForm 连接到上面的 here 文档感到困惑.

I am stuck at this point. I am confused at how to connect my method calcForm to the here document above.

对此的任何帮助将不胜感激!

Any help with this would be greatly appreciated!

谢谢!

推荐答案

看起来您正在考虑将heredoc 作为一种模板,您定义一次,内置字符串插值,然后重用.不是.与任何字符串定义一样,字符串插值会在定义变量时即时发生.

It looks like you're thinking of a heredoc as a sort of template, that you define once, with string interpolations built in, and then reuse. It isn't. As with any string definition, the string interpolation happens on the fly when the variable is defined.

所以你会这样做

def calcForm(left,op,right,result)
   <<HERE
     <input type="text" name="left" value=#{left}> 
     <select name="op">
     <option value="add" #{op}>+</option>
     <option value="mul" #{op}>*</option> 
     </select>
     <input type="text" name="right" value=#{right}> 
     =
     #{result}
   HERE
end

然而,更好的方法可能是ERB,这更像你上面想到的;即它是一个模板.

However, a better approach for what you're trying to do might be ERB, which works more like what you had in mind above; i.e. it is a template.

require 'erb'
template = ERB.new <<HERE
         <input type="text" name="left" value=<%=left%>> 
         <select name="op">
         <option value="add" <%=op%>>+</option>
         <option value="mul" <%=op%>>*</option> 
         </select>
         <input type="text" name="right" value=<%=right%>> 
         =
         <%=result%>
         HERE

def calcForm(left,op,right,result)
   template.result(binding)    
end

注意这里的binding是一个神奇的词,意思是评估当前上下文中的表达式";即使用当前定义的变量(传入的参数).

Note that binding here is a magic word that means "evaluate the expression in the current context"; i.e. with the currently defined variables (the parameters that were passed in).

这篇关于ruby 这里的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆