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"


775
776
777
778
# File 'lib/source/ruby.rb', line 775

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

Instance Method Details

#<(other_module) ⇒ Object



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

def <(other_module)
end

#<=(other_module) ⇒ Object



783
784
# File 'lib/source/ruby.rb', line 783

def <=(other_module)
end

#<=>(other_module) ⇒ Object



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

def <=>(other_module)
end

#===(object) ⇒ Object



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

def ===(object)
end

#>(other_module) ⇒ Object



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

def >(other_module)
end

#>=(other_module) ⇒ Object



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

def >=(other_module)
end

#alias_method(new_name, old_name) ⇒ Object



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

def alias_method(new_name, old_name)
end

#ancestorsObject



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

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



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

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

#attr(attribute, writer = false) ⇒ Object



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

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



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

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



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

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



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

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



852
853
# File 'lib/source/ruby.rb', line 852

def class_eval(&block)
end

#class_variable_defined?Boolean

Returns:

  • (Boolean)


855
856
# File 'lib/source/ruby.rb', line 855

def class_variable_defined?
end

#class_variable_getObject



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

def class_variable_get
end

#class_variable_setObject



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

def class_variable_set
end

#class_variablesObject



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

def class_variables
end

#const_defined?Boolean

Returns:

  • (Boolean)


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

def const_defined?
end

#const_get(sym) ⇒ Object



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

def const_get(sym)
end

#const_set(sym, object) ⇒ Object



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

def const_set(sym, object)
end

#constantsObject



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

def constants
end

#define_method(sym, &block) ⇒ Object



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

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


908
909
910
911
912
913
914
915
916
917
918
# File 'lib/source/ruby.rb', line 908

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


945
946
# File 'lib/source/ruby.rb', line 945

def extended(object)
end

#hashObject

:nodoc:



948
949
950
# File 'lib/source/ruby.rb', line 948

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"


971
972
973
974
# File 'lib/source/ruby.rb', line 971

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)


976
977
# File 'lib/source/ruby.rb', line 976

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


1000
1001
# File 'lib/source/ruby.rb', line 1000

def included(other_module)
end

#included_modulesObject



1003
1004
# File 'lib/source/ruby.rb', line 1003

def included_modules
end

#instance_methodObject



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

def instance_method
end

#instance_methodsObject



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

def instance_methods
end

#method_defined?Boolean

Returns:

  • (Boolean)


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

def method_defined?
end

#module_eval(&block) ⇒ Object



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

def module_eval(&block)
end

#nameObject



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

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

#remove_class_variable(sym) ⇒ Object



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

def remove_class_variable(sym)
end

#remove_const(sym) ⇒ Object



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

def remove_const(sym)
end

#remove_method(sym) ⇒ Object



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

def remove_method(sym)
end

#to_sObject

Return a string representing this module or class.



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

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