Class: Module
Overview
A Module
is a collection of methods and constants. The methods in a module may be instance methods or module methods. Instance methods appear as methods in a class when the module is included, module methods do not. Conversely, module methods may be called without creating an encapsulating object, while instance methods may not. (See Module#module_function
)
In the descriptions that follow, the parameter sym refers to a symbol, which is either a quoted string or a Symbol
(such as :name
).
module Mod
include Math
CONST = 1
def my_method
# ...
end
end
Mod.class #=> Module
Mod.constants #=> ["E", "PI", "CONST"]
Mod.instance_methods #=> ["my_method"]
Direct Known Subclasses
Instance Method Summary collapse
- #<(other_module) ⇒ Object
- #<=(other_module) ⇒ Object
- #<=>(other_module) ⇒ Object
- #===(obj) ⇒ Object
- #>(other_module) ⇒ Object
- #>=(other_module) ⇒ Object
- #alias_method(new_name, old_name) ⇒ Object
- #ancestors ⇒ Object
-
#append_features(mod) ⇒ Object
call-seq: append_features(mod) -> mod.
- #attr(attribute, writer = false) ⇒ Object
- #attr_accessor(*symbols) ⇒ Object
- #attr_reader(*symbols) ⇒ Object
- #attr_writer(*symbols) ⇒ Object
- #class_eval(&block) ⇒ Object
- #class_variable_defined? ⇒ Boolean
- #class_variable_get ⇒ Object
- #class_variable_set ⇒ Object
- #class_variables ⇒ Object
- #const_defined? ⇒ Boolean
- #const_get(sym) ⇒ Object
- #const_set(sym, object) ⇒ Object
- #constants ⇒ Object
- #define_method(sym, &block) ⇒ Object
-
#extend_object(obj) ⇒ Object
call-seq: extend_object(obj) -> obj.
-
#extended(object) ⇒ Object
call-seq: extended(extended_obj).
-
#hash ⇒ Object
:nodoc:.
-
#include(*modules) ⇒ Object
call-seq: mod.include(module, …) -> mod.
- #include?(other_module) ⇒ Boolean
-
#included(other_module) ⇒ Object
call-seq: included(including_module).
- #included_modules ⇒ Object
-
#initialize(module_name, &block) ⇒ Module
constructor
call-seq: Module.new(module_name) -> mod Module.new(module_name) {|mod| block } -> mod.
- #instance_method ⇒ Object
- #instance_methods ⇒ Object
- #method_defined? ⇒ Boolean
- #module_eval(&block) ⇒ Object
- #name ⇒ Object
- #remove_class_variable(sym) ⇒ Object
- #remove_const(sym) ⇒ Object
- #remove_method(sym) ⇒ Object
-
#to_s ⇒ Object
Return a string representing this module or class.
Constructor Details
#initialize(module_name, &block) ⇒ Module
call-seq:
Module.new(module_name) -> mod
Module.new(module_name) {|mod| block } -> mod
Creates a new module. Unlike in Ruby, where you need only assign the module object to a constant in order to give it a name, in Red you must also pass the module name as a string argument. If a block is given, it is passed the module object, and the block is evaluated in the context of this module using module_eval
.
Greeter = Module.new do
def say_hi
"hello"
end
def say_bye
"goodbye"
end
end
a = "my string"
a.extend(Greeter) #=> "my string"
a.say_hi #=> "hello"
a.say_bye #=> "goodbye"
789 790 791 792 |
# File 'lib/source/ruby.rb', line 789 def initialize(module_name, &block) `this.__name__=module_name.__value__||module_name` `this.prototype={}` end |
Instance Method Details
#<(other_module) ⇒ Object
794 795 |
# File 'lib/source/ruby.rb', line 794 def <(other_module) end |
#<=(other_module) ⇒ Object
797 798 |
# File 'lib/source/ruby.rb', line 797 def <=(other_module) end |
#<=>(other_module) ⇒ Object
800 801 |
# File 'lib/source/ruby.rb', line 800 def <=>(other_module) end |
#===(obj) ⇒ Object
803 804 805 |
# File 'lib/source/ruby.rb', line 803 def ===(obj) `obj.m$is_a_bool(this)` end |
#>(other_module) ⇒ Object
807 808 |
# File 'lib/source/ruby.rb', line 807 def >(other_module) end |
#>=(other_module) ⇒ Object
810 811 |
# File 'lib/source/ruby.rb', line 810 def >=(other_module) end |
#alias_method(new_name, old_name) ⇒ Object
813 814 |
# File 'lib/source/ruby.rb', line 813 def alias_method(new_name, old_name) end |
#ancestors ⇒ Object
816 817 |
# File 'lib/source/ruby.rb', line 816 def ancestors end |
#append_features(mod) ⇒ Object
call-seq:
append_features(mod) -> mod
When another module includes this module, this module calls append_features
with the module that included this one as the mod parameter. If this module has not already been added to mod or one of its ancestors, this module adds its constants, methods, and module variables to mod. See also Module#include
.
FIX: Incomplete
829 830 831 832 833 834 |
# File 'lib/source/ruby.rb', line 829 def append_features(mod) `Red.donateMethodsToSingleton(this,mod)` `Red.donateMethodsToClass(this.prototype,mod.prototype)` `Red.donateConstantsToClass(this,mod)` return `mod` end |
#attr(attribute, writer = false) ⇒ Object
836 837 838 839 840 841 |
# File 'lib/source/ruby.rb', line 836 def attr(attribute, writer = false) `var a=attribute.__value__` `f1=this.prototype['m$'+a]=function(){return this['i$'+arguments.callee._name];};f1._name=a` `if(writer){f2=this.prototype['m$'+a.__value__+'_eql']=function(x){return this['i$'+arguments.callee._name]=x;};f2._name=a;}` return nil end |
#attr_accessor(*symbols) ⇒ Object
843 844 845 846 847 848 849 850 |
# File 'lib/source/ruby.rb', line 843 def attr_accessor(*symbols) `for(var i=0,l=symbols.length;i<l;++i){ var a=symbols[i].__value__; f1=this.prototype['m$'+a]=function(){return this['i$'+arguments.callee._name];};f1._name=a; f2=this.prototype['m$'+a+'_eql']=function(x){return this['i$'+arguments.callee._name]=x;};f2._name=a; }` return nil end |
#attr_reader(*symbols) ⇒ Object
852 853 854 855 856 857 858 |
# File 'lib/source/ruby.rb', line 852 def attr_reader(*symbols) `for(var i=0,l=symbols.length;i<l;++i){ var a=symbols[i].__value__; f=this.prototype['m$'+a]=function(){return this['i$'+arguments.callee._name];};f._name=a; }` return nil end |
#attr_writer(*symbols) ⇒ Object
860 861 862 863 864 865 866 |
# File 'lib/source/ruby.rb', line 860 def attr_writer(*symbols) `for(var i=0,l=symbols.length;i<l;++i){ var a=symbols[i].__value__; f=this.prototype['m$'+a+'_eql']=function(x){return this['i$'+arguments.callee._name]=x;};f._name=a; }` return nil end |
#class_eval(&block) ⇒ Object
868 869 |
# File 'lib/source/ruby.rb', line 868 def class_eval(&block) end |
#class_variable_defined? ⇒ Boolean
871 872 |
# File 'lib/source/ruby.rb', line 871 def class_variable_defined? end |
#class_variable_get ⇒ Object
874 875 |
# File 'lib/source/ruby.rb', line 874 def class_variable_get end |
#class_variable_set ⇒ Object
877 878 |
# File 'lib/source/ruby.rb', line 877 def class_variable_set end |
#class_variables ⇒ Object
880 881 |
# File 'lib/source/ruby.rb', line 880 def class_variables end |
#const_defined? ⇒ Boolean
883 884 |
# File 'lib/source/ruby.rb', line 883 def const_defined? end |
#const_get(sym) ⇒ Object
886 887 |
# File 'lib/source/ruby.rb', line 886 def const_get(sym) end |
#const_set(sym, object) ⇒ Object
889 890 |
# File 'lib/source/ruby.rb', line 889 def const_set(sym, object) end |
#constants ⇒ Object
892 893 |
# File 'lib/source/ruby.rb', line 892 def constants end |
#define_method(sym, &block) ⇒ Object
895 896 |
# File 'lib/source/ruby.rb', line 895 def define_method(sym, &block) end |
#extend_object(obj) ⇒ Object
call-seq:
extend_object(obj) -> obj
Extends the specified object by adding this module’s constants and methods (which are added as singleton methods). This is the callback method used by Object#extend
.
module Picky
def Picky.extend_object(obj)
unless obj.is_a?(String)
puts "Picky methods added to #{o.class}"
super
else
puts "Picky won't give its methods to a String"
end
end
end
[1,2,3].extend(Picky)
"1,2,3".extend(Picky)
produces:
Picky methods added to Array
Picky won't give its methods to a String
924 925 926 927 928 929 930 931 932 933 934 |
# File 'lib/source/ruby.rb', line 924 def extend_object(obj) `var tp=this.prototype` `for(var x in tp){ if(x.slice(0,2)=='m$'){ var f=function(){var m=arguments.callee;return m.__methodSource__[m.__methodName__].apply(m.__methodReceiver__,arguments) }; f.__methodName__=x;f.__methodSource__=tp;f.__methodReceiver__=obj; obj[x]=f; }; }` return `obj` end |
#extended(object) ⇒ Object
call-seq:
extended(extended_obj)
Callback invoked whenever the receiver is used to extend an object’s singleton class. Default callback is empty; override the method definition in your modules to activate.
module Mod
def self.extended(obj)
puts "#{obj} was extended with #{self}"
end
end
class Thing
def to_s
"My object"
end
end
Thing.new.extend(Mod)
produces:
My object was extended with Mod
961 962 |
# File 'lib/source/ruby.rb', line 961 def extended(object) end |
#hash ⇒ Object
:nodoc:
964 965 966 |
# File 'lib/source/ruby.rb', line 964 def hash # :nodoc: `'c_'+#{self.to_s}` end |
#include(*modules) ⇒ Object
call-seq:
mod.include(module, ...) -> mod
Invokes append_features
and the callback included
on each given module in turn, passing mod as the argument.
module Mod
def hello
"Hello from Mod"
end
end
class Klass
include Mod
end
k = Klass.new
k.hello #=> "Hello from Mod"
987 988 989 990 |
# File 'lib/source/ruby.rb', line 987 def include(*modules) `for(var i=0,l=modules.length;i<l;++i){var mod=modules[i];mod.m$append_features(this);mod.m$included(this);mod.__includers__[this.__name__]=true;}` return self end |
#include?(other_module) ⇒ Boolean
992 993 |
# File 'lib/source/ruby.rb', line 992 def include?(other_module) end |
#included(other_module) ⇒ Object
call-seq:
included(including_module)
Callback invoked whenever the receiver is included in another module or class. Default callback is empty; override the method definition in your modules to activate.
module Mod
def self.included(base)
puts "#{self} included in #{base}"
end
end
module Enumerable
include Mod
end
produces:
Mod included in Enumerable
1016 1017 |
# File 'lib/source/ruby.rb', line 1016 def included(other_module) end |
#included_modules ⇒ Object
1019 1020 |
# File 'lib/source/ruby.rb', line 1019 def included_modules end |
#instance_method ⇒ Object
1022 1023 |
# File 'lib/source/ruby.rb', line 1022 def instance_method end |
#instance_methods ⇒ Object
1025 1026 |
# File 'lib/source/ruby.rb', line 1025 def instance_methods end |
#method_defined? ⇒ Boolean
1028 1029 |
# File 'lib/source/ruby.rb', line 1028 def method_defined? end |
#module_eval(&block) ⇒ Object
1031 1032 |
# File 'lib/source/ruby.rb', line 1031 def module_eval(&block) end |
#name ⇒ Object
1034 1035 1036 |
# File 'lib/source/ruby.rb', line 1034 def name `$q(this.__name__.replace(/\\./g,'::'))` end |
#remove_class_variable(sym) ⇒ Object
1038 1039 |
# File 'lib/source/ruby.rb', line 1038 def remove_class_variable(sym) end |
#remove_const(sym) ⇒ Object
1041 1042 |
# File 'lib/source/ruby.rb', line 1041 def remove_const(sym) end |
#remove_method(sym) ⇒ Object
1044 1045 |
# File 'lib/source/ruby.rb', line 1044 def remove_method(sym) end |
#to_s ⇒ Object
Return a string representing this module or class.
1048 1049 1050 |
# File 'lib/source/ruby.rb', line 1048 def to_s `$q(this.__name__.replace(/\\./g,'::'))` end |