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"


751
752
753
754
# File 'lib/source/ruby.rb', line 751

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

Instance Method Details

#<(other_module) ⇒ Object



756
757
# File 'lib/source/ruby.rb', line 756

def <(other_module)
end

#<=(other_module) ⇒ Object



759
760
# File 'lib/source/ruby.rb', line 759

def <=(other_module)
end

#<=>(other_module) ⇒ Object



762
763
# File 'lib/source/ruby.rb', line 762

def <=>(other_module)
end

#===(object) ⇒ Object



765
766
# File 'lib/source/ruby.rb', line 765

def ===(object)
end

#>(other_module) ⇒ Object



768
769
# File 'lib/source/ruby.rb', line 768

def >(other_module)
end

#>=(other_module) ⇒ Object



771
772
# File 'lib/source/ruby.rb', line 771

def >=(other_module)
end

#alias_method(new_name, old_name) ⇒ Object



774
775
# File 'lib/source/ruby.rb', line 774

def alias_method(new_name, old_name)
end

#ancestorsObject



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

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



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/source/ruby.rb', line 790

def append_features(mod)
  `var tp=this.prototype,mp=mod.prototype`
  `for(var x in tp){
    if(x.slice(0,2)=='m$'){
      var f=function(){return arguments.callee._source[arguments.callee._name].apply(this,arguments) };
      f._source=tp;
      f._name=x;
      mp[x]=f;
    };
  }`
  return `mod`
  
  
  #`for(x in this.prototype){#{other_module}.prototype[x]=this.prototype[x];}`
  #`for(#{x} in #{self}){if(x.slice(0,2)=='');}` # FIGURE THIS OUT
  #return other_module
end

#attr(a, writer = false) ⇒ Object



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

def attr(a, writer = false)
  `this.prototype['m$'+a._value]=function(){return this['i$'+a._value];}`
  `if(writer){this.prototype['m$'+a._value+'Eql']=function(x){return this['i$'+a._value]=x;};}`
  return nil
end

#attr_accessor(*symbols) ⇒ Object



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

def attr_accessor(*symbols)
  `for(var i=0,l=symbols.length;i<l;++i){
    var a=symbols[i]._value;
    this.prototype['m$'+a]=function(){return this['i$'+a];};
    this.prototype['m$'+a+'Eql']=function(x){return this['i$'+a]=x;};
  }`
  return nil
end

#attr_reader(*symbols) ⇒ Object



823
824
825
826
827
828
829
# File 'lib/source/ruby.rb', line 823

def attr_reader(*symbols)
  `for(var i=0,l=symbols.length;i<l;++i){
    var a=symbols[i]._value;
    this.prototype['m$'+a]=function(){return this['i$'+a];};
  }`
  return nil
end

#attr_writer(*symbols) ⇒ Object



831
832
833
834
835
836
837
# File 'lib/source/ruby.rb', line 831

def attr_writer(*symbols)
  `for(var i=0,l=symbols.length;i<l;++i){
    var a=symbols[i]._value;
    this.prototype['m$'+a+'Eql']=function(x){return this['i$'+a]=x;};
  }`
  return nil
end

#class_eval(&block) ⇒ Object



839
840
# File 'lib/source/ruby.rb', line 839

def class_eval(&block)
end

#class_variable_defined?Boolean



842
843
# File 'lib/source/ruby.rb', line 842

def class_variable_defined?
end

#class_variable_getObject



845
846
# File 'lib/source/ruby.rb', line 845

def class_variable_get
end

#class_variable_setObject



848
849
# File 'lib/source/ruby.rb', line 848

def class_variable_set
end

#class_variablesObject



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

def class_variables
end

#const_defined?Boolean



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

def const_defined?
end

#const_get(sym) ⇒ Object



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

def const_get(sym)
end

#const_set(sym, object) ⇒ Object



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

def const_set(sym, object)
end

#constantsObject



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

def constants
end

#define_method(sym, &block) ⇒ Object



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

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


895
896
897
898
899
900
901
902
903
904
905
# File 'lib/source/ruby.rb', line 895

def extend_object(obj)
  `for(var x in this.prototype){
    if(x.slice(0,2)=='m$'){
      var f=function(){return arguments.callee._source[arguments.callee._name].apply(obj,arguments) };
      f._source=this.prototype;
      f._name=x;
      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


932
933
# File 'lib/source/ruby.rb', line 932

def extended(object)
end

#hashObject

:nodoc:



935
936
937
# File 'lib/source/ruby.rb', line 935

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"


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

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

#include?(other_module) ⇒ Boolean



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

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


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

def included(other_module)
end

#included_modulesObject



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

def included_modules
end

#instance_methodObject



993
994
# File 'lib/source/ruby.rb', line 993

def instance_method
end

#instance_methodsObject



996
997
# File 'lib/source/ruby.rb', line 996

def instance_methods
end

#method_defined?Boolean



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

def method_defined?
end

#module_eval(&block) ⇒ Object



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

def module_eval(&block)
end

#nameObject



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

def name
  `$q(this._name)`
end

#remove_class_variable(sym) ⇒ Object



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

def remove_class_variable(sym)
end

#remove_const(sym) ⇒ Object



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

def remove_const(sym)
end

#remove_method(sym) ⇒ Object



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

def remove_method(sym)
end

#to_sObject

Return a string representing this module or class.



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

def to_s
  `$q(this._name)`
end