1

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
chobo
  • 4,830
  • 5
  • 23
  • 36

1 Answers1

3

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 }
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367