Prawn gem を使用して PDF を生成するクラスに取り組んでいます。私はいくつかの同様の方法を持っています。それらはすべて同じ行から始まります。コードは次のとおりです。
module PDFGenerator
class MatchTeamInfo
include Prawn::View
def initialize(match)
@match = match
@output = Prawn::Document.new page_layout: :landscape
defaults
header
footer
end
def render
@output.render
end
def defaults
@output.instance_exec do
font_size 16
text 'hola'
end
end
def header
@output.instance_exec do
bounding_box [bounds.left, bounds.top], :width => bounds.width do
text "Fulbo", align: :center, size: 32
stroke_horizontal_rule
move_down(5)
end
end
end
def footer
@output.instance_exec do
bounding_box [bounds.left, bounds.bottom + 25], :width => bounds.width do
stroke_horizontal_rule
move_down(5)
text "Tu sitio favorito!", align: :center
end
end
end
end
end
すべての方法で回避@output.instance_exec
し、ブロックのようなものを使用する方法はありますか? やってみましたが、うまくいきません。このようなことはできますか?
def apply
@output.instance_exec do
yield
end
end
コード ブロックをどのように定義すればよいですか?