Welcome to day 23 of the 49 Days of Ruby! 🎉
Yesterday, we discussed duck typing in Ruby. If duck typing is the idea that what an Object is doesn't really matter as long as it responds to a method, then types and type checking is the opposite.
There are other languages that do require types to be defined as part of writing the code. Those languages include Java, C#, TypeScript, and others.
Before we look at Ruby's implementation, let's ask the first question: What is a type?
Let's say I had the following in my code:
"Hi there! This is some text."
What would you instinctively say its type is?
If you said a
String you would be right!
type of the above text is indeed a
That's what we mean when we talk about types. How about this:
That number is an
Integer. Another example:
That number with a decimal point is a
When we say we are enforcing types, what we mean is we are defining during the time we write the code what data type the object or method is, and once it is defined, it cannot be changed. In other words, ducks from the land of duck typing need to exit the room.
The most recent major release of Ruby, version 3.0, includes support at the language level for types.
What does it look like? This is an example from the README for RBS the gem used for static typing in Ruby:
attr_reader login: String
attr_reader email: String
By now, the code above will mostly look familiar. We have a class instantiation of
User, followed by two
Did you notice the addition of the
: and the
String in each
That's the syntax for declaring the type of those attributes. In this example, they are being declared as strings.
You can also declare types for a method, more from the example:
def initialize: (login: String, email: String) -> void
Here we have the
#initialize method for the
User class. We see that it accepts two parameters:
attr_reader declarations before.
What about the
-> syntax? That's how one declares the
type of the return value of the method. Every method returns something. By stating
void we are saying that the value can be
nil and we ought not to care about it.
There is so much more we can say about types in Ruby, but we're going to end it here today! For further exploration check out:
See you tomorrow!
Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.