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.
  3. NASA's Dragonfly probe to Saturn's Titan in 2028 - Nov. 26, 2024, 2:58 p.m.
  4. Rails ActiveRecord has a .sole method - Nov. 26, 2024, 11:15 p.m.
  5. pick is the single-value version of pluck in Rails - Nov. 26, 2024, 11:24 p.m.

Next: Rails migrations can include an up_only part

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