This week, Ruby’s development team released the first preview build of their long-awaited major update to version 3.0!

Among the countless cool changes and additions — including some fantastic general performance boosts and a powerful new concurrency/parallel processing efficiency model — a new built-in language has emerged for static type definitions: Ruby (Type) Signature language, or RBS.

Static Typing

Ruby is a dynamically typed language, which means the interpreter tries to infer the data type of variables and object properties at runtime. This generally leads to programs being more dynamic and easier (faster) to code, and the interpreter/compiler loading code faster. However, the need for type inference and its associated costs, as well as the difficulty in detecting errors at compile time with dynamically typed code, have caused a modern programming trend to move towards statically typed languages.

Static typing makes the development experience much smoother. Bugs are far easier to detect, especially with modern IDEs and their linting as you write. For most statically typed languages, their more explicit nature leads to faster-compiled code (more efficient resulting machine code.)

Popular dynamically typed languages have begun to follow suit. Python’s typing library for optional static type definitions has gained significant traction since its introduction in Python 3.5. Microsoft’s statically typed JavaScript superset TypeScript has seen a meteoric rise in popularity in recent years and is basically an industry standard at this point.

Types in Ruby

With the upcoming version 3.0, Ruby has plans to integrate optional static typing as well!

The devs have created a “foundational type signature language” gem in the new version preview called RBS. The new .rbs files are analogous to TypeScript’s .d.ts type definition files. These files contain all your various modules’ type definitions. By keeping them in separate files, your actual Ruby code need not be changed and you can quickly and easily add the benefits of static types to your Ruby programs!

In addition to traditional type definition syntax that looks pretty much as you would expect a Ruby version to look, RBS also implements interfaces for safer “guided” Duck Typing, “union types,” and “method overloading” to account for non-uniform Ruby types, as well as a few other cool features.

#typing #programming #software-engineering #typescript #ruby

Static Typing in Ruby 3.0
1.40 GEEK