time.current.tap {| t |T.Change Hour:10}在Rails 4.2中不起作用



我尝试了

[107] pry(main)> t=Time.current
=> Tue, 02 Jul 2019 19:19:05 KST +09:00
[108] pry(main)> t.tap{|tt| tt.change hour: 10 }
=> Tue, 02 Jul 2019 19:19:05 KST +09:00
[109] pry(main)> t
=> Tue, 02 Jul 2019 19:19:05 KST +09:00

小时没有改变...

我期望

[110] pry(main)> t.tap{|tt| tt.change hour: 10 }
Tue, 02 Jul 2019 10:00:00 KST +09:00

真的我想要

[111] pry(main)> Time.current.tap{|t| t.change(hour: 10, min: t.min)}
Tue, 02 Jul 2019 10:19:00 KST +09:00

change返回一个新的时间对象,因此tap忽略了此值。您可以使用Tap的兄弟yield_self(Ruby 2.5 (

t.yield_self{|tt| tt.change hour: 10 }

最新更新