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"


780
781
782
783
# File 'lib/source/ruby.rb', line 780

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

Instance Method Details

#<(other_module) ⇒ Object



785
786
# File 'lib/source/ruby.rb', line 785

def <(other_module)
end

#<=(other_module) ⇒ Object



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

def <=(other_module)
end

#<=>(other_module) ⇒ Object



791
792
# File 'lib/source/ruby.rb', line 791

def <=>(other_module)
end

#===(obj) ⇒ Object



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

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

#>(other_module) ⇒ Object



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

def >(other_module)
end

#>=(other_module) ⇒ Object



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

def >=(other_module)
end

#alias_method(new_name, old_name) ⇒ Object



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

def alias_method(new_name, old_name)
end

#ancestorsObject



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

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



820
821
822
823
824
# File 'lib/source/ruby.rb', line 820

def append_features(mod)
  `Red.donateMethodsToSingleton(this,mod)`
  `Red.donateMethodsToClass(this.prototype,mod.prototype)`
  return `mod`
end

#attr(attribute, writer = false) ⇒ Object



826
827
828
829
830
831
# File 'lib/source/ruby.rb', line 826

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



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

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



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

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



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

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



858
859
# File 'lib/source/ruby.rb', line 858

def class_eval(&block)
end

#class_variable_defined?Boolean

Returns:

  • (Boolean)


861
862
# File 'lib/source/ruby.rb', line 861

def class_variable_defined?
end

#class_variable_getObject



864
865
# File 'lib/source/ruby.rb', line 864

def class_variable_get
end

#class_variable_setObject



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

def class_variable_set
end

#class_variablesObject



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

def class_variables
end

#const_defined?Boolean

Returns:

  • (Boolean)


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

def const_defined?
end

#const_get(sym) ⇒ Object



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

def const_get(sym)
end

#const_set(sym, object) ⇒ Object



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

def const_set(sym, object)
end

#constantsObject



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

def constants
end

#define_method(sym, &block) ⇒ Object



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

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


914
915
916
917
918
919
920
921
922
923
924
# File 'lib/source/ruby.rb', line 914

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


951
952
# File 'lib/source/ruby.rb', line 951

def extended(object)
end

#hashObject

:nodoc:



954
955
956
# File 'lib/source/ruby.rb', line 954

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"


977
978
979
980
# File 'lib/source/ruby.rb', line 977

def include(*modules)
  `for(var i=0,l=modules.length;i<l;++i){var mod=modules[i];mod.m$appendFeatures(this);mod.m$included(this);mod.__includers__[this.__name__]=true;}`
  return self
end

#include?(other_module) ⇒ Boolean

Returns:

  • (Boolean)


982
983
# File 'lib/source/ruby.rb', line 982

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


1006
1007
# File 'lib/source/ruby.rb', line 1006

def included(other_module)
end

#included_modulesObject



1009
1010
# File 'lib/source/ruby.rb', line 1009

def included_modules
end

#instance_methodObject



1012
1013
# File 'lib/source/ruby.rb', line 1012

def instance_method
end

#instance_methodsObject



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

def instance_methods
end

#method_defined?Boolean

Returns:

  • (Boolean)


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

def method_defined?
end

#module_eval(&block) ⇒ Object



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

def module_eval(&block)
end

#nameObject



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

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

#remove_class_variable(sym) ⇒ Object



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

def remove_class_variable(sym)
end

#remove_const(sym) ⇒ Object



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

def remove_const(sym)
end

#remove_method(sym) ⇒ Object



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

def remove_method(sym)
end

#to_sObject

Return a string representing this module or class.



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

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