gpt4 book ai didi

ruby - 用代码包围 Ruby 方法

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:24 27 4
gpt4 key购买 nike

我有很多这样的方法:

def enableMotors
@posIface.Lock 1
@posIface.data.cmdEnableMotors = 1
@posIface.Unlock
end

def goTo (pos)
@posIface.Lock 1
@posIface.data.cmdVelocity.pos = pos
@posIface.Unlock
end

我想为 :before_filter 和 :after_filter 创建功能,或者以任何其他方式让这段代码尽可能保持干爽。我不想仅仅为此依赖 Rails 或其他繁重的东西。

最佳答案

您真的需要一个完整的 :before :after 回调系统吗?或者这对您来说足够了吗?

def with_lock(&block)
@posIface.Lock 1
yield
@posIface.Unlock
end

def enableMotors
with_lock { @posIface.data.cmdEnableMotors = 1 }
end

def goTo (pos)
with_lock { @posIface.data.cmdVelocity.pos = pos }
end

关于ruby - 用代码包围 Ruby 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1104984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com