Category Archives: Fun Coding Fact

Ruby : Monkey Patching!

Fun Coding Word of the Week!! : Monkey Patching
Funny word comes with interesting results.
Ruby is always a fun language to develop. Monkey patching allows you to open a previous class and override or append new methods to said class.
Below is an example:

class MonkeyAround
  def feedMe
    puts "Im full"
  end

  def sleep
    puts "zzz"
  end
end

class MonkeyAround
  #override .. now when MonkeyAround instance is called with method "feedme" ... "Im hungry!" will display
  def feedMe
    puts "Im hungry!"
  end

  # New method attached to "MonkeyAround" class. 
  def play
    puts "Play Time!"
  end
end

george = MonkeyAround.new
george.feedMe # outputs "Im Hungry!"
george.play # outputs "Play Time!"
george.sleep # outputs "zzz"