[Ruby] Modules -- Ex
Namespacing
Create a module named GameUtils
and place the lend_to_friend
method inside the module. Changelend_to_friend
to a class method by prefixing it with self.
.
You won't need to require this module since it'll be inside the same file (already required), but you will have to namespace your method call.
class Game def initialize(name) @name = name end end
Answer:
module GameUtils def self.lend_to_friend(game, friend_email) end end game = Game.new("Contra") GameUtils.lend_to_friend(game, "gregg@codeschool.com")
Mixin
Re-open the Game
class and include theGameUtils
module so its methods are exposed as instance methods. Make sure to do this before it is called.
Answer:
class Game include GameUtils end game = Game.new("contra") game.lend_to_friend("Gregg")
Extend
Good job! Now expose the methods from the GameUtils module as class methods of the Game class.
class Game extend GameUtils end Game.find_all_from_user("Gregg")
Object Extend
Extend the single game
object with the Playable module, so we can call the play
method on it.
game = Game.new("Contra") game.extend(Playable) game.play
Hook Methods
Define a new self.included
method hook for the LibraryUtils
module which will extend the ClassMethods
on the passed in class. Also, since we'll now be extending ClassMethods
when LibraryUtils
is included, remove duplicate code in the AtariLibrary
class.
module LibraryUtils def add_game(game) end def remove_game(game) end module ClassMethods def search_by_game_name(name) end end end class AtariLibrary include LibraryUtils extend LibraryUtils::ClassMethods end
Answer:
module LibraryUtils def self.included(base) base.extend(ClassMethods) end def add_game(game) end def remove_game(game) end module ClassMethods def search_by_game_name(name) end end end class AtariLibrary include LibraryUtils end
ActiveSupport::Concern - Part I
Now refactor the following code to use ActiveSupport::Concern's ability to expose class methods from a module.
module LibraryUtils def self.included(base) base.extend(ClassMethods) end def add_game(game) end def remove_game(game) end module ClassMethods def search_by_game_name(name) end end end
Answer: ActiveSupport::Concern can replace self.included
module LibraryUtils extend ActiveSupport::Concern def add_game(game) end def remove_game(game) end module ClassMethods def search_by_game_name(name) end end end
ActiveSupport::Concern - Part II
Call the included
method from inside the LibraryUtils module and pass in a block that calls theload_game_list
class method.
module LibraryUtils extend ActiveSupport::Concern included do load_game_list end def add_game(game) end def remove_game(game) end module ClassMethods def search_by_game_name(name) end def load_game_list end end end
ActiveSupport::Concern - Part III
Make sure the AtariLibrary class includes only the LibraryUtils module and let ActiveSupport::Concern take care of loading its dependencies. Then, refactor the self.included
method on LibraryUtils to use theincluded
method.
module LibraryLoader extend ActiveSupport::Concern module ClassMethods def load_game_list end end end module LibraryUtils def self.included(base) base.load_game_list end end class AtariLibrary include LibraryLoader include LibraryUtils end
Answer:
module LibraryLoader extend ActiveSupport::Concern module ClassMethods def load_game_list end end end module LibraryUtils extend ActiveSupport::Concern include LibraryLoader included do load_game_list end end class AtariLibrary include LibraryUtils end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具