Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Can module methods be aliased?

arev

Sponsor

Simple question :) Let's say I want to add one line of code to Input.update. Can it be done without rewriting the whole thing?
 
I don't know about the modules, but the hidden classes can't be aliased because you don't know what to aliasing.

That. But I mean, the modules are classes too, so you can alias them.
 

Zeriab

Sponsor

Sure you can.
The way I do it is by using an anonymous inner class. There might be others. In fact there probably is.
Code:
module Input
  class << self
    unless self.method_defined?(:zeriab_myscript_input_update)
      alias_method(:zeriab_myscript_input_update, :update)
    end
    def update
      @x ||= 0
      @x += 1
      p @x if @x % 100 == 0
      zeriab_myscript_input_update
    end
  end
end

@mephisto: Hidden classes can be aliased. If you don't know what to alias just use reflection to get that information.

*hugs*
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top