[EN] include vs extend in Ruby
Thâm / March 15, 2023
1 min read • ––– lượt xem
module Commentable
def submit
"Commented"
end
end
class Post
include Commentable # `submit` method is an instance method
end
class Post
include Commentable # `submit` method is an instance method
extend Commentable # `submit` method is a class method
end
class Post
extend Commentable # `submit` method is a class method
end
# `submit` method can be an instance method with extend keyword when doing:
post = Post.new
post.extend Commentable
module A
def a
p "method a"
end
def self.c
p "method c"
end
end
class B
include A
end
B.new.a # print 'method a'
B.c # undefined method c
A.c # print 'method c'
Đăng ký nhận thông báo qua email khi có bài viết mới
0 người đăng ký