1. 程式人生 > >Why Ruby community encourages Duck Typing

Why Ruby community encourages Duck Typing

Why the Ruby community encourages Duck Typing

In this article, we’re going to explore the following topics:

  • statically typed languages
  • dynamically typed languages
  • duck typing design principle

Introduction

Each value of a program is associated to a type.

Depending on the language, variables’ type can be defined statically or dynamically.

Ruby relies on a principle that is called Duck Typing.

So, let’s have a look to what are statically, dynamically typed language.

Also, We’ll answer the following question:

Why encourage the use of the Duck Typing design principle in Ruby?

Statically typed languages

In a statically typed language, a variable’s type is static.

This means that when you associate a variable to a type then you cannot change it anymore.

In this case, typing is associated with the variable rather than the value it refers to.

Some languages, such as C, force each variable to be associated to a bunch of constraints at declaration.

These constraints are specific to each existing type in C

In the above C program we can see that the x variable is associated to the int type — for integer.

So this type will be associated to this variable for all of its lifetime.

No matter the value that we assign to the variable, the variable’s type will remain int.

Note that statically typed languages are generally compiled language, as the compiler can type-check each variable through multiple defined constraints at compile time.

Dynamically typed languages

In a dynamically typed language, variables’ types are dynamic.

This means that when you associate a variable to a type then you can change it whenever you want.

Actually, typing is associated with the value it assumes rather than the variable itself.

Some languages, such as Python, are dynamically typed languages

Here the website variable first refers to a string and then to a boolean.

So the type is defined at runtime and Python doesn’t do any type checking.

In dynamically typed languages, you can think of a variable as a box that contains a pointer to a value of any type.

So what about Ruby?

Duck typing design

First, Ruby is also a dynamically typed language.

Typing is defined at runtime.

There is no implicit type-checking in Ruby.

The Ruby programming language is based on conventions. It lets a maximum of control to the developer. The developer is responsible of the code that it produces.

Of course, Ruby provides a bunch of constraints to help the developer.

Ruby isn’t a No Man’s Land.

Furthermore, the Core Team proposes a set of principles that each developer decides to adopt or not.

One of them is the Duck Typing design principle.

Duck Typing is based on the well known Duck Test

When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.

That we can translate In Ruby as following

If an object quacks like a duck (or acts like an array), just go ahead and treat it as a duck (or an array)

Let’s have an example to illustrate the above quote

Here the a variable responds to map and returns the expected object. So it’s legitimate to think that the a variable is an Array.

Conclusion (with a quote of Matz)

In conclusion, I will share with you an answer of Matz to a question that I asked him this week:

Why encouraging the use of Duck Typing in Ruby?

Unlike static and dynamic typing of variables (and expressions), duck typing is not a language feature, but a design principle which is a combination of dynamic typing and object-oriented programming. Since Ruby is a dynamically typed object-oriented programming language, it is natural for the Ruby community to encourage duck typing.
Yukihiro “Matz” Matsumoto

Voilà!

⬇️⬇️⬇️FEEL FREE TO VISIT MY NEW WEBSITE ⬇️⬇️⬇️