Duck Typing

You may have noticed that in Ruby you don’t explicitly declare the types of variables or methods. Whether the particular value of a variable is correct for the messages being passed to it’s evaluated at run time when the message is sent.

Folks tend to react to this in one of two ways. Some like this flexibility and feel comfortable writing code with dynamically typed variables and methods. Others get nervous when they think about all those objects floating around unconstrained. If you’ve come to Ruby from a language such as C#, Java, or TypeScript, where you’re used to giving all your variables and methods an explicit type, you may feel that Ruby is just too permissive for writing “real” applications.

It isn’t.

We’d like to spend ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.