Sha256: b2a72ba81b6685ddede862d54e149d8e54aa5160700387329675d48a55e95d2f

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

## Module#class_extend

    require 'facets/module/class_extend'

Module using class_extend

    mix = Module.new do
      def i ; end
      class_extend do
        def n ; 42   ; end
        def s ; self ; end
      end
    end

Extends module

    mix.n.assert == 42
    mix.s.assert == mix

Has expected methods

    mix.instance_methods.map{ |m| m.to_s }.assert == ['i']

transfers class methods to including class

    mod = Module.new do
      include mix
    end
    mod.n.assert == 42
    mod.s.assert == mod

Doesn't overwrite instance methods

    c = Class.new do
      include mix
      def n ; 11 ; end
    end
    c.new.n.assert == 11

Is overridable via including module

    mod = Module.new do
      include mix
      class_extend do
        def n ; super + 1 ; end
      end
    end
    mod.n.assert == 43  # notice the difference!
    mod.s.assert == mod

Transfers class methods thru multiple levels of include

    alt = Module.new do
      include mix
      class_extend do
        def n ; super + 1 ; end
      end
    end
    mod = Module.new do
      include alt
    end
    mod.n.assert == 43
    mod.s.assert == mod

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 demo/core/module/class_extend.md
facets-3.1.0 demo/core/module/class_extend.md