/ Published in: Ruby
An example which illustrates the ways of extending a class by class methods.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# encoding: utf-8 # Two ways of extending a class by class methods. module M module ClassMethods def one 'ok!' end def two 'ok two!' end end end # I class TestOne class << self include M::ClassMethods end end # II class TestTwo extend M::ClassMethods end puts [ TestOne.one, #=> ok! TestOne.two #=> ok two! ] puts [ TestTwo.one, #=> ok! TestTwo.two #=> ok two! ]