[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
复制代码

 

posted @   Zhentiw  阅读(321)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 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工具
点击右上角即可分享
微信分享提示