Class: Module

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

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

Class

Instance Method Summary collapse

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"


788
789
790
791
# File 'lib/source/ruby.rb', line 788

def initialize(module_name, &block)
  `this.__name__=module_name.__value__||module_name`
  `this.prototype={}`
end

Instance Method Details

#<(other_module) ⇒ Object



793
794
# File 'lib/source/ruby.rb', line 793

def <(other_module)
end

#<=(other_module) ⇒ Object



796
797
# File 'lib/source/ruby.rb', line 796

def <=(other_module)
end

#<=>(other_module) ⇒ Object



799
800
# File 'lib/source/ruby.rb', line 799

def <=>(other_module)
end

#===(obj) ⇒ Object



802
803
804
# File 'lib/source/ruby.rb', line 802

def ===(obj)
  `obj.m$is_a_bool(this)`
end

#>(other_module) ⇒ Object



806
807
# File 'lib/source/ruby.rb', line 806

def >(other_module)
end

#>=(other_module) ⇒ Object



809
810
# File 'lib/source/ruby.rb', line 809

def >=(other_module)
end

#alias_method(new_name, old_name) ⇒ Object



812
813
# File 'lib/source/ruby.rb', line 812

def alias_method(new_name, old_name)
end

#ancestorsObject



815
816
# File 'lib/source/ruby.rb', line 815

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



828
829
830
831
832
833
# File 'lib/source/ruby.rb', line 828

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



835
836
837
838
839
840
# File 'lib/source/ruby.rb', line 835

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



842
843
844
845
846
847
848
849
# File 'lib/source/ruby.rb', line 842

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



851
852
853
854
855
856
857
# File 'lib/source/ruby.rb', line 851

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



859
860
861
862
863
864
865
# File 'lib/source/ruby.rb', line 859

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



867
868
# File 'lib/source/ruby.rb', line 867

def class_eval(&block)
end

#class_variable_defined?Boolean

Returns:

  • (Boolean)


870
871
# File 'lib/source/ruby.rb', line 870

def class_variable_defined?
end

#class_variable_getObject



873
874
# File 'lib/source/ruby.rb', line 873

def class_variable_get
end

#class_variable_setObject



876
877
# File 'lib/source/ruby.rb', line 876

def class_variable_set
end

#class_variablesObject



879
880
# File 'lib/source/ruby.rb', line 879

def class_variables
end

#const_defined?Boolean

Returns:

  • (Boolean)


882
883
# File 'lib/source/ruby.rb', line 882

def const_defined?
end

#const_get(sym) ⇒ Object



885
886
# File 'lib/source/ruby.rb', line 885

def const_get(sym)
end

#const_set(sym, object) ⇒ Object



888
889
# File 'lib/source/ruby.rb', line 888

def const_set(sym, object)
end

#constantsObject



891
892
# File 'lib/source/ruby.rb', line 891

def constants
end

#define_method(sym, &block) ⇒ Object



894
895
# File 'lib/source/ruby.rb', line 894

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


923
924
925
926
927
928
929
930
931
932
933
# File 'lib/source/ruby.rb', line 923

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


960
961
# File 'lib/source/ruby.rb', line 960

def extended(object)
end

#hashObject

:nodoc:



963
964
965
# File 'lib/source/ruby.rb', line 963

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"


986
987
988
989
# File 'lib/source/ruby.rb', line 986

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

Returns:

  • (Boolean)


991
992
# File 'lib/source/ruby.rb', line 991

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


1015
1016
# File 'lib/source/ruby.rb', line 1015

def included(other_module)
end

#included_modulesObject



1018
1019
# File 'lib/source/ruby.rb', line 1018

def included_modules
end

#instance_methodObject



1021
1022
# File 'lib/source/ruby.rb', line 1021

def instance_method
end

#instance_methodsObject



1024
1025
# File 'lib/source/ruby.rb', line 1024

def instance_methods
end

#method_defined?Boolean

Returns:

  • (Boolean)


1027
1028
# File 'lib/source/ruby.rb', line 1027

def method_defined?
end

#module_eval(&block) ⇒ Object



1030
1031
# File 'lib/source/ruby.rb', line 1030

def module_eval(&block)
end

#nameObject



1033
1034
1035
# File 'lib/source/ruby.rb', line 1033

def name
  `$q(this.__name__.replace(/\\./g,'::'))`
end

#remove_class_variable(sym) ⇒ Object



1037
1038
# File 'lib/source/ruby.rb', line 1037

def remove_class_variable(sym)
end

#remove_const(sym) ⇒ Object



1040
1041
# File 'lib/source/ruby.rb', line 1040

def remove_const(sym)
end

#remove_method(sym) ⇒ Object



1043
1044
# File 'lib/source/ruby.rb', line 1043

def remove_method(sym)
end

#to_sObject

Return a string representing this module or class.



1047
1048
1049
# File 'lib/source/ruby.rb', line 1047

def to_s
  `$q(this.__name__.replace(/\\./g,'::'))`
end