Hacker News new | past | comments | ask | show | jobs | submit login

Does ruby have its equivalent to typescript, with type annotations? The language sounds interesting but I tend not to give dynamically typed languages the time of day





> Does ruby have its equivalent to typescript, with type annotations?

Ruby has a first party external type definition format (RBS) as well as third-party typecheckers that check ruby against RBS definitions.

There is probably more use of the older, all third-party typing solution (Sorbet) though.


There's https://sorbet.org/ but it's not clear whether it has much adoption.

I continue to think it was a big mistake not to add syntactic support for type annotations into the base language. python did this right; annotations are not enforced by the interpreter, but are accessible both by external tools as part of the AST and bytecode, and by the running program via introspection, so tools and libraries can do all sorts of interesting things with them.

having to add annotations in a separate header file is simply too high friction to get widespread adoption.


IMHO (and I don't expect most people to agree but please be tolerant of my opinion!) annotations are annoying busywork that clutter my code and exist just to make people feel smart for “““doing correctness”””. The only check I find useful is nil or not-nil, and any halfway-well-designed interface should make it impossible for some unexpected object type to end up in the wrong place anyway. For anything less than halfway-well-defined, you have bigger issues than a lack of type annotation.

edit: I am quite fond of `case ::Ractor::receive; when SomeClass then …; when SomeOtherClass then …; end` as the main pattern for my Ractors though :)


as your codebase and number of collaborators get larger, it's super useful to have the type checker be able to tell you "hey, you said your function arg could be a time or an int, but you are calling time-specific methods on it" or conversely "the function you are calling says it accepts time objects but you are passing it an int"

also once you get into jit compilation you can do some nice optimisations if you can treat a variable type as statically known rather than dynamic.

and finally, even if you're not writing python at scale it can be very nice to use the type annotations to document your function parameters.


> also once you get into jit compilation you can do some nice optimisations if you can treat a variable type as statically known rather than dynamic.

This is something I hadn't considered. Thanks for mentioning it :)


Sorbet is the most mature option. RBS barely has any tooling, while Sorbet works well.

It definitely isn't at the level of Typescript adoption, even relatively speaking. And it's more clunky than Typescript. But it works well enough to be valuable.


It's interesting that RBS support is used by IRB for type completion.

There is also work to settle on an inline form of RBS so I could see it taking over from Sorbet annotations in the future.


If you like the Ruby syntax (but want a statically typed language), you might want to take a look at Crystal: https://crystal-lang.org/

> Crystal is statically typed and type errors are caught early by the compiler, eliminating a range of type-related errors at runtime.

> Yet type annotations are rarely necessary, thanks to powerful type inference. This keeps the code clean and feels like a dynamic language.

Why does it remain relatively unpopular and what can be done so that more people get to use it?


> Why does it remain relatively unpopular and what can be done so that more people get to use it?

Because Ruby-ish syntax without Ruby’s semantics or ecosystem isn’t actually all that big of selling point, and if people want a statically typed language, there are plenty of options with stronger ecosystems, some of which have some Ruby-ish stntactic features.


> Does ruby have its equivalent to typescript, with type annotations?

Someone recommended me this, so I might even spread the word further https://github.com/soutaro/rbs-inline?tab=readme-ov-file#rbs...


This is the main thing keeping me from going back to Ruby. I don’t want to go back to the stone age where there’s no or poor static analysis

If you're looking for static typing a dynamic language is going to be a poor fit. I find a place for both. I love Rust, but trying to write a tool that consumed a GraphQL API with was a brutal exercise in frustation. I'd say that goes for typing of JSON or YAML or whatever structured format in general. It's refreshing being able to just work with data in the form I already know it's in. Ruby can be an incredibly productive language to work with.

If you're looking for static analysis in general, please note that there are mature tools available. Rubocop¹ is probably the most popular and allows for linting and code formatting. Brakeman² is a vulnerability scanner for Rails. Sorbet³ is a static type checker.

The tooling is there if you want to try things out. But, if you want a statically typed language then that's a debate that's been going since the dawn of programming language design. I doubt it's going to get resolved in this thread.

¹ - https://github.com/rubocop/rubocop

² - https://brakemanscanner.org/

³ - https://sorbet.org/


I’ve used rubocop and sorbet. But now that I’ve used TypeScript it’s clear there’s no comparison. TS will even analyze your regex patterns. Every update gets better. I’m eagerly waiting for the day they add analysis for array length and ranged numbers.

I want to use Lua for more tasks



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: