Using Ruby Class and Module Differently

Asked 2 years ago, Updated 2 years ago, 34 views

I made some apps with rails.

I'm getting used to class, but I don't really understand the module.
There seems to be no difference from the class, but what is the difference and how do you use it differently?

ruby-on-rails ruby

2022-09-29 22:51

1 Answers

Create a module when you want to synthesize methods that span multiple classes instead of inheritance.

class=>can be instantiated.inheritable.
module=>Unable to instantiate.Namespace and method container.The method can be mixed in.

Rails often use ActiveSupport::Concern to create modules.

[Rails] ActiveSupport::Concern Reasons for Existence - Qiita

For example, if you implement only the upload function as concern on the controller that uploads files, it's a versatile application.


2022-09-29 22:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.