在 Ruby 中,可以对从文件读取的数据执行字符串插值吗?
在 Ruby 中,您可以引用字符串内的变量,并且它们在运行时进行插值。
例如,如果您声明一个变量 foo
等于 "Ted"
并声明一个字符串 "Hello, #{foo}"
它会插入到 <代码>“你好,特德”。
我无法弄清楚如何对从文件读取的数据执行神奇的 "#{}"
插值。
在伪代码中,它可能看起来像这样:
interpolated_string = File.new('myfile.txt').read.interpolate
但是最后一个 interpolate
方法不存在。
In Ruby you can reference variables inside strings and they are interpolated at runtime.
For example if you declare a variable foo
equals "Ted"
and you declare a string "Hello, #{foo}"
it interpolates to "Hello, Ted"
.
I've not been able to figure out how to perform the magic "#{}"
interpolation on data read from a file.
In pseudo code it might look something like this:
interpolated_string = File.new('myfile.txt').read.interpolate
But that last interpolate
method doesn't exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用 IO.read(filename) 将文件读入字符串,然后将结果用作格式字符串 (http://www.ruby-doc.org/core-2.0/String.html#method-i-25):
myfile.txt:
fill_in_name。 rb:
在终端运行“ruby fill_in_name.rb”的结果:
You can read the file into a string using IO.read(filename), and then use the result as a format string (http://www.ruby-doc.org/core-2.0/String.html#method-i-25):
myfile.txt:
fill_in_name.rb:
result of running "ruby fill_in_name.rb" at the terminal:
已经给出了 2 个最明显的答案,但如果由于某种原因没有给出答案,则有格式运算符:
您拥有较旧的(但经过时间考验的) %s 魔法,而不是 #{} 魔法 .. 。
The 2 most obvious answers have already been given, but if they don't to it for some reason, there's the format operator:
where instead of the #{} magic you have the older (but time-tested) %s magic ...
使用 daniel-lucraft 的答案作为我的基础(因为他似乎是唯一回答这个问题的人)我决定以稳健的方式解决这个问题。 您将在下面找到此解决方案的代码。
对于输入,
您将获得输出
输入
将引发 NameError 异常。 并且输入
将引发 ArgumentError 异常,抱怨输入包含所有可用的分隔符。
Using daniel-lucraft's answer as my base (as he seems to be the only one that answered the question) I decided to solve this problem in a robust manner. Below you will find the code for this solution.
for the input
you get the output
The input
will raise a NameError exception. And the input
will raise and ArgumentError exception complaining that the input contained all available delimiter characters.
不妨将我自己的解决方案加入其中。
缺点是您要计算的表达式可能还包含 { } ,因此正则表达式可能需要改进。
Might as well throw my own solution into the mix.
The weakness is that the expression you want to evaluate may have further { } in it, so the regex should probably be improved.
@andrewgrimm 的答案是正确的,但不完整。 Facets gem 具有
String
扩展,包括interpolate
,这正是原始问题所要求的。几乎完全按照OP的要求使用它。 请注意
require
语句。这是一个完整的工作示例:
显示:
The answer by @andrewgrimm was on the right track, but incomplete. The Facets gem has
String
extensions, includinginterpolate
, which is precisely what the original question asked for.Use it almost exactly as the OP requested. Note the
require
statement.Here is a complete working example:
Displays:
我认为这可能是在 Ruby 1.9.x 中执行您想要的操作的最简单、最安全的方法(sprintf 在 1.8.x 中不支持按名称引用):使用 Kernel.sprintf 的“按名称引用”功能。 例子:
I think this might be the easiest and safest way to do what you want in Ruby 1.9.x (sprintf doesn't support reference by name in 1.8.x): use Kernel.sprintf feature of "reference by name". Example:
好吧,我同意 stesch 在这种情况下使用 erb 的回答。 但你可以像这样使用 eval 。 如果 data.txt 包含内容:
那么您可以像这样加载和插值:
并且
结果
将是:Well, I second stesch's answer of using erb in this situation. But you can use eval like this. If data.txt has contents:
Then you can load and interpolate like this:
And
result
will be:您可以使用
erb< 而不是插值/代码>
。 此博客给出了 ERB 使用的简单示例,
Kernel#eval
也可以使用。 但大多数时候您想要使用简单的模板系统,例如erb
。Instead of interpolating, you could use
erb
. This blog gives simple example of ERB usage,Kernel#eval
could be used, too. But most of the time you want to use a simple template system likeerb
.Ruby Facets 提供了 String#interpolate 方法:
Ruby Facets provides a String#interpolate method: