What is the difference between a class and a module in Ruby?

A class is an object-oriented programming construct that is used to define a type of object. It defines both the data and the behavior of the objects of that type. Classes are the building blocks of object-oriented programming.

Example:

class Dog
attr_accessor :name, :breed
def initialize(name, breed)
@name = name
@breed = breed
end
end

A module is a collection of methods and constants that can be included in classes. It is used to provide a namespace and prevent name clashes between similarly named methods.

Example:

module Dog
def bark
puts “Woof!”
end
end

What is the difference between a class and a module?

A class is a blueprint for creating objects, while a module is a collection of methods and constants that can be used by other classes.

For example, a class might be used to create objects that represent a car, such as a Ford Mustang. The class would define the attributes of the car, such as the make, model, and color.

A module, on the other hand, might contain methods for calculating fuel efficiency or calculating the cost of repairs. These methods could be used by any class, not just the car class.