2 items tagged “Ruby”
2024
📝 Ruby Sorbet is too verbose, can the syntax be improved? #
Awhile back when I was ramping up on Typescript, I was leaning into the benefits of types. In the Ruby world, Sorbet is the most common way to add types so I gave that a shot. In the end, I ripped it out. I spent more time wrangling type checking then the time it spent catching bugs, and one of the issues was the verbose syntax. But recently I came up with a novel solution to this problem. First, here is the verbose syntax — you end up declaring all your arguments twice:
sig { params(name: T.untyped, nickname: String).returns(Integer) } def greet(name, nickname) puts "Hello, #{name}! Your nickname: #{nickname}" name.length end
💡 TIL: Ruby can chain methods and right-assign #
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) } => totalfrom davetron