python - Ruby 中的函数装饰器,与 Python 中一样

标签 python ruby function functional-programming decorator

有没有一种方法可以像在 Python 中那样在 Ruby 中装饰函数?也就是说,在每个函数的开头(和结尾?)执行一些操作。

像这样:http://programmingbits.pythonblogs.com/27_programmingbits/archive/50_function_decorators.html

最佳答案

如果你所说的函​​数是闭包,你可以使用 block :

def foo
  puts 'before code'

  yield

  puts 'after code'
end

foo { puts 'here is the code' }

关于python - Ruby 中的函数装饰器,与 Python 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304168/

相关文章:

python - 在不对结果进行排序的情况下计算数据帧的模式

python - 双星号错误: Invalid Syntax

python - 带有回调 : access violation on exit 的 ctypes

mysql - 安装用于 Ruby 的 MySQL gem 以及 JRuby gem

javascript - 将包含变量的对象发送给函数

PHP : Display a message using a function without using echo

python - Python 3 中的 strip 不是 "working"吗?

ruby - 如何升级到 ruby​​ 1.9.2

ruby-on-rails - Rails 3 中的嵌套表单更新将外键设置为 NULL

c++ - 从 int 到 int 的无效转换*