Keith Schacht’s Weblog

Subscribe

💡 TIL: Ruby can chain methods and right-assign

29th October 2024

I just learned that ruby supports re-writing this:

total = plus_shipping(with_taxes(subtotal(items)))

As:

subtotal(items).then { |subtotal| with_taxes(subtotal) }.then { |total| plus_shipping(total) } => total
from davetron

This is Ruby can chain methods and right-assign by Keith Schacht, posted on 29th October 2024.

Part of series Today I Learned

  1. Ruby can chain methods and right-assign - Oct. 29, 2024, 8:23 p.m.
  2. Rails migrations can include an up_only part - Oct. 29, 2024, 8:29 p.m.

Next: Rails migrations can include an up_only part

Previous: I'm loving Rails Stimulus; check out this example.