Class: Module

Inherits:
Object show all
Defined in:
lib/roebe/core/module.rb

Overview

#

Module is also a class, thus you can extend it.

#

Instance Method Summary collapse

Instance Method Details

#attr_accessor2(*attr) ⇒ Object

#

attr_accessor2

Makes a new attr_accessor.

Test like so:

class Xyz
  attr_accessor2 :yo
end; x = Xyz.new;x.yo="yap";x.yo
#


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/roebe/core/module.rb', line 20

def attr_accessor2(*attr)
  attr.each { |a|
    ivar = :"@#{a}"
    define_method(a) {
      instance_variable_get(ivar)
    }
    define_method(:"#{a}=") { |value|
      instance_variable_set(ivar, value)
    }
  }
end

#attr_my_accessor(*attr) ⇒ Object

#

attr_my_accessor

This also makes a var? reader.

Test it like so (example):

class Xyz

attr_my_accessor :yo

end; x = Xyz.new; x.yo = “yap”; x.yo?

Or in one line:

class Xyz; attr_my_accessor :yo;end; x=Xyz.new;x.yo="yap oh yoh";x.yo?
#


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roebe/core/module.rb', line 46

def attr_my_accessor(*attr) 
  attr.each { |a|
    ivar = :"@#{a}"
    [
      :"#{a}?", a
    ].each { |method|
      define_method(method) { instance_variable_get(ivar) }
    }
    define_method(:"#{a}=") { |value|
      instance_variable_set(ivar, value)
    }
  }
end

#attr_my_reader(*attr) ⇒ Object

#

attr_my_reader

This makes a ? getter in addition to the normal getter. In other words it combines .foo and .foo?

Usage example follows:

class Foobar

attr_my_reader :yo
define_method(:initialize) { @yo = 22}

end; x = Foobar.new; x.yo?; x.yo

#


73
74
75
76
77
78
79
# File 'lib/roebe/core/module.rb', line 73

def attr_my_reader(*attr) 
  attr.each { |a|
    ivar = :"@#{a}"
    define_method(a)        { instance_variable_get(ivar) }
    define_method(:"#{a}?") { instance_variable_get(ivar) }
  }
end

#extend_with(mod) ⇒ Object

#

extend_with

Example:

String.extend_with(Term::ANSIColor)
#


87
88
89
# File 'lib/roebe/core/module.rb', line 87

def extend_with(mod)
  include(mod)
end