Time.current.tap{|t| t.change hour: 10 } is not working in Rails 4.2

44 views Asked by At

I tried

[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

hour is not changed...

I expect

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

Really I want

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

There are 1 answers

1
Sergio Tulentsev On

change returns a new time object, so this value is ignored by tap. You can use tap's brother, yield_self (ruby 2.5+)

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