Ruby块 - 在yield之后返回yield yield运行代码

时间:2017-08-03 09:38:21

标签: ruby yield ruby-block

我想返回public void uploadFile() throws XWPFConverterException, IOException { InputStream in = new FileInputStream(new File("path/to/file")); XWPFDocument document = new XWPFDocument(in); XHTMLOptions options = XHTMLOptions.create(); XHTMLConverter.getInstance().convert(document, out, options); String html = out.toString(); html = html.replaceAll("<html>", "<html xmlns='http://www.w3.org/1999/xhtml' " + "\n" + " xmlns:h='http://java.sun.com/jsf/html' " + "\n" + " xmlns:f='http://java.sun.com/jsf/core' " + "\n" + " xmlns:p='http://primefaces.org/ui ' " + "\n" + " xmlns:ui='http://java.sun.com/jsf/facelets' " + "\n" + " xmlns:pe='http://primefaces.org/ui/extensions' " + "\n" + " xmlns:components='http://java.sun.com/jsf/composite/components' >"); html = parseHTML(html, extractPhoto(document)); html = html.replaceAll("<body>", "<h:body>").replaceAll("</body>", "</h:body>"); saveHelpFile(html.getBytes("ISO-8859-2")); } 的输出,但也要在yield之后执行代码,是否有更“正确”的方式?:

yield

1 个答案:

答案 0 :(得分:4)

您可以使用tap

def myblock
  yield.tap { puts 'after yield' }
end

myblock { 'my yield' }
# after yield
#=> my yield