Class: Class

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

Overview

As in Ruby, classes in Red are first-class objects – each is an instance of class Class.

When a new class is created (typically using class Name ... end), an object of type Class is created and assigned to a global constant (Name in this case). When Name.new is called to create a new object, the new method in Class is run by default. This can be demonstrated by overriding new in Class:

class Class
  alias saved_new new
  def new(*args)
    puts "Creating a new #{self.name}"
    saved_new(*args)
  end
end

class Name
end

n = Name.new

produces:

Creating a new Name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#<, #<=, #<=>, #===, #>, #>=, #alias_method, #ancestors, #append_features, #attr, #attr_accessor, #attr_reader, #attr_writer, #class_eval, #class_variable_defined?, #class_variable_get, #class_variable_set, #class_variables, #const_defined?, #const_get, #const_set, #constants, #define_method, #extend_object, #extended, #hash, #include, #include?, #included, #included_modules, #initialize, #instance_method, #instance_methods, #method_defined?, #module_eval, #name, #remove_class_variable, #remove_const, #remove_method, #to_s

Constructor Details

This class inherits a constructor from Module

Class Method Details

.new(class_name, superclass = Object) ⇒ Object

call-seq:

Class.new(class_name, superclass = Object) -> class

Creates a new class with the given __superclass___ (or Object if no superclass is given). Unlike in Ruby, where you need only assign the class object to a constant in order to give it a name, in Red you must also pass the class name as a string argument.

FIX: Incomplete



1089
1090
1091
1092
# File 'lib/source/ruby.rb', line 1089

def self.new(class_name, superclass = Object)
  `Red._class(class_name.__value__,superclass,function(){})`
  return `window['c$'+class_name.__value__]`
end

Instance Method Details

#allocateObject

call-seq:

klass.allocate -> object

Returns a new object that is an instance of klass. This method is used internally by a Class object’s new method and should not be called directly.



1101
1102
1103
# File 'lib/source/ruby.rb', line 1101

def allocate
  `new(this)()`
end

#inherited(subclass) ⇒ Object

call-seq:

inherited(subclass)

Callback invoked whenever a subclass of the current class is created.

class Foo
  def self.inherited(subclass)
    puts "New subclass: #{subclass}"
  end
end

class Bar < Foo
end

class Baz < Bar
end

produces:

New subclass: Bar
New subclass: Baz


1127
1128
# File 'lib/source/ruby.rb', line 1127

def inherited(subclass)
end

#newObject

call-seq:

klass.new(args, ...) -> object

Calls allocate to create a new object of class klass, then invokes that object’s initialize method, passing it args.

class Foo
  def initialize(a,b)
    @a = a
    @b = b
  end

  def values
    "a,b: [%s,%s]" % [@a, @b]
  end
end

foo = Foo.new(1,2)    #=> #<Foo:0x3cc57a>
foo.values            #=> "a,b: [1,2]"


1150
1151
1152
1153
1154
# File 'lib/source/ruby.rb', line 1150

def new
  `var result=this.m$allocate()`
  `this.prototype.m$initialize.apply(result,arguments)`
  return `result`
end

#superclassObject

call-seq:

klass.superclass -> class or nil

Returns the superclass of klass, or nil.

Class.superclass    #=> Module
Module.superclass   #=> Object
Object.superclass   #=> nil


1165
1166
1167
# File 'lib/source/ruby.rb', line 1165

def superclass
  `this.__superclass__`
end