关于使用“打印”方法在 ERB 中进行元编程等

发布于 2024-09-10 13:35:31 字数 967 浏览 6 评论 0原文

我通过控制台使用 ERB 进行元编程(用于数学软件)。例如,我有文件 test.erb 包含

text line before ruby
<%=  'via <%='  %>
<%  print 'print'  %>
<%  puts  'puts'   %>
text line after ruby

当我通过 $ erb test.erb 解析它时,我得到以下输出

printputs
text line before ruby
via <%=


text line after ruby

我对此并不感到惊讶,但想知道是否有没有一种好方法可以捕获 print 方法的输出并将其放在 ERB 模板中调用它的位置?

text line before ruby
via <%=
print
puts
text line after ruby

想象一下,我有一个复杂的结构,我更愿意打印而不是在 <%= %> 内的字符串中收集输出。


更新

只是为了说明Brian的答案:

text line before ruby
<%= '<%=' %>
% print 'print'
% puts 'puts'
% E = _erbout
% E << '_erbout'+"\n"
text line after ruby

解析文件$ erb test.erb

printputs
text line before ruby
<%=
_erbout
text line after ruby

I am using ERB via console for metaprogramming (for math software). For example, I have file test.erb containing

text line before ruby
<%=  'via <%='  %>
<%  print 'print'  %>
<%  puts  'puts'   %>
text line after ruby

When I parse it by $ erb test.erb, I get the following output

printputs
text line before ruby
via <%=


text line after ruby

I am not surprised by it, but wonder if there is a good way to catch output of print method and put it at the place where it is called in the ERB template?

text line before ruby
via <%=
print
puts
text line after ruby

Imagine that I have a complex construction, where I would prefer to print instead of collecting output in a string inside <%= %>.


Update

Just to illustrate the answer of Brian:

text line before ruby
<%= '<%=' %>
% print 'print'
% puts 'puts'
% E = _erbout
% E << '_erbout'+"\n"
text line after ruby

Parsing the file $ erb test.erb:

printputs
text line before ruby
<%=
_erbout
text line after ruby

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北斗星光 2024-09-17 13:35:31

不确定这是否对您的特定情况有帮助,但请考虑使用 _erbout 方法查看一些示例,

text line before ruby
<%=  'via <%='  %>
<% _erbout << 'print' %>
<% _erbout << 'puts'  %>
text line after ruby

希望这能让您有所收获。

Not certain if this helps in your particular case, but consider looking at some examples using the _erbout method

text line before ruby
<%=  'via <%='  %>
<% _erbout << 'print' %>
<% _erbout << 'puts'  %>
text line after ruby

Hope this gets you somewhere.

策马西风 2024-09-17 13:35:31

作为一种选择,可以重新定义 Kernel#p 方法:

file: p_for_erb.rb

module Kernel
  alias :p_super :p

  def p *args
    if args.empty?
      @@_erbout
    elsif args.first.class == Hash
      @@_erbout = args.first[:init]
    else
      args.each { |a| @@_erbout << a }
    end
  end
end

...然后执行类似的操作:

file: mytest.erb

text before ruby
% require 'p_for_erb'
% p init: _erbout # to initialize class variable @@_erbout
% p "my p output"

Command $ erb mytest.erb产生

text before ruby
my p output

As an option, one can redefine Kernel#p method:

file: p_for_erb.rb

module Kernel
  alias :p_super :p

  def p *args
    if args.empty?
      @@_erbout
    elsif args.first.class == Hash
      @@_erbout = args.first[:init]
    else
      args.each { |a| @@_erbout << a }
    end
  end
end

...and then do similar to this:

file: mytest.erb

text before ruby
% require 'p_for_erb'
% p init: _erbout # to initialize class variable @@_erbout
% p "my p output"

Command $ erb mytest.erb produces

text before ruby
my p output
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文