Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Duck Typing?
3.
Why is it called Duck Typing?
4.
Example
4.1.
Code
4.2.
Output
4.3.
Explanation
5.
Why should we care about Duck Typing?
6.
Frequently Asked Questions
6.1.
What is Type Checking?
6.2.
What is Duck Typing?
6.3.
Is Ruby a dynamically typed language?
6.4.
What is ‘===’ in Ruby?
6.5.
Name some of the conditional statements ruby supports.
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Type Checking and Duck Typing in Ruby

Author Ankit Kumar
0 upvote

Introduction

The high-level, interpreted programming language Ruby supports a number of different programming paradigms. It was created with a focus on productivity and simplicity in the programming. And this article will discuss such a simple topic in ruby, i.e. Type Checking and Duck Typing in Ruby. You may have come across the term "duck typing" if you have worked with any dynamic programming languages, such as Ruby, JavaScript, Python, etc.

Introduction image

Ruby is a dynamically type-checked language that uses the "duck typing" method: “If something swims and quacks like a duck, it must be a duck.” 

What is Duck Typing?

The concept of duck typing holds that an object's capabilities and behavior should be decided by its public interface rather than by its type. Let's get this with the help of a simple example. Assume that the bird we are talking about can swim and quack; these are characteristics of a duck. Let's say there is another type of bird that fits the description of a duck since it can swim and quack. In such a case, we will refer to the bird as a duck. Duck typing is what it is.

Why is it called Duck Typing?

Alex Martelli created the term "duck typing" in the delirious months following Y2K to describe the fuzzy behaviour of dynamic typing. Programmers had less control over the exact type of the objects they were utilising due to dynamic typing than they had with the concrete type assurance of C++ and Java. Early adopters of dynamic languages responded by trying to enforce static typing through constant type checking of objects. Martelli discouraged this conduct with a straightforward analogy:

Instead of determining if anything is a duck, determine whether it QUACKS like a duck, WALKS like a duck, etc., according to the specific subset of duck-like behaviour you need to use for language games.

Example

Consider an object duck that can swim and quack. Let's stick with Duck's illustration.

Code

class Duck
  def swim
    puts 'Duck swims'
  end
  def quack
    puts 'Duck quacks'
  end
end
class Dove
  def swim 
    puts 'Dove swims'
  end
  def quack
    puts 'Dove quacks'
  end
end 
class BirdActions
attr_reader :birds
def initialize
  @birds = []
  duck = Duck.new()
  dove = Dove.new()
  @birds.push(duck)
  @birds.push(dove)
end
def swim
  birds.each do | bird |
    bird.swim
  end
end
def quack
  birds.each do | bird |
    bird.quack
  end
end

end

action = BirdActions.new()
action.quack
action.swim
You can also try this code with Online Ruby Compiler
Run Code

Output

Duck quacks
Dove quacks
Duck swims
Dove swims

Explanation

We have two bird objects: a duck and a dove, and a third class that is basically a class that aggregates birds and performs operations on them.

Quack and swim are the two techniques used by both bird groups. The fascinating portion comes next. In the BirdActions class, we create instances of the Duck and Goose classes and save them in an array called "birds."

Here, we implement the duck typing model.

This is possible with a dynamic type language because we may supply two different types of objects with methods that have the same name but execute different actions depending on the situation. Since it shares the public interface methods, we don't need to verify kind_of? (this method verifies the instance of a class) in this case. Doing so prevents conditional statements like if-else and switch, resulting in simpler code.

Why should we care about Duck Typing?

Duck typing is a very beautiful design method when done properly. Your program will have a lot more freedom to develop and change over time if you let the functionality of your code exclusively depend on open interfaces rather than the underlying objects.

Let's discuss some FAQs related to the topic.

Frequently Asked Questions

What is Type Checking?

The type checker performs type checking by confirming that a construct's type (constant, variable, array, list, or object) corresponds to what is anticipated in the context of its usage. This makes it more likely that specific code issues will be found and reported.

What is Duck Typing?

The concept of duck typing holds that an object's capabilities and behaviour should be decided by its public interface rather than by its type.

Is Ruby a dynamically typed language?

There is no data type declaration in Ruby, so Ruby is a dynamically typed language.

What is ‘===’ in Ruby?

When a case statement's “when” clause contains an equality test, Ruby's === operator is utilised to check for equality.

Name some of the conditional statements ruby supports.

Ruby supports if, if-else, unless case statements. These all have their own significance according to the need of the programmer.

Conclusion

In this article, we've extensively discussed type checking and duck typing in Ruby. We discussed the type checking and duck typing in Ruby along with an explanatory example. We have implemented the code for the given examples. 

When you have finished reading Type Checking and Duck Typing In Ruby? Are you not eager to study and research more about Ruby? Chill. Coding Ninjas made your task easy. To learn, see Constants in RubyArrays in RubyHow to invoke global functions in Ruby? or Ranges in Ruby.

You can check the official documentation of Ruby too.

Happy Coding!

Thankyou Image
Live masterclass