Class: Object

Inherits:
BasicObject
Defined in:
lib/source/ruby.rb

Overview

Object is the parent class of all classes in Red. Its methods are therefore available to all objects unless explicitly overridden.

In the descriptions of Object‘s methods, the parameter sym refers to either a quoted string or a Symbol (such as :name).

Instance Method Summary collapse

Constructor Details

#initializeObject

:nodoc:



126
127
# File 'lib/source/ruby.rb', line 126

def initialize # :nodoc:
end

Instance Method Details

#==(other) ⇒ Object

call-seq:

obj == other      -> true or false
obj.eql?(other)   -> true or false
obj.equal?(other) -> true or false

Equality – at the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

The eql? method returns true if obj and other have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses may override this behavior.



147
148
149
# File 'lib/source/ruby.rb', line 147

def ==(other)
  `this._objectId==other._objectId`
end

#===(other) ⇒ Object

call-seq:

obj === other -> true or false

Case Equality – for class Object, effectively the same as calling ==, but typically overridden by descendents to provide meaningful semantics in case statements.



158
159
160
# File 'lib/source/ruby.rb', line 158

def ===(other)
  `this._objectId==other._objectId`
end

#=~Object

call-seq:

obj =~ other -> false

Pattern Match – overridden by descendents (notably Regexp and String) to provide meaningful pattern-match semantics.



168
169
170
# File 'lib/source/ruby.rb', line 168

def =~
  false
end

#__id__Object

call-seq:

obj.__id__    -> integer
obj.object_id -> integer

Returns an integer identifier for obj. The same number will be returned on all calls to _\id\_ for a given object, and no two active objects will share an id.



180
181
182
# File 'lib/source/ruby.rb', line 180

def __id__
  `this._objectId`
end

#__send__(method, *args) ⇒ Object

call-seq:

obj.__send__(sym [, args...]) -> object
obj.send(sym [, args...])     -> object

Invokes the method identified by sym, passing it any arguments specified. Use _\send\_ if the name send clashes with an existing method in obj.

class Klass
  def hello(*args)
    "Hello " + args.join(' ')
  end
end

k = Klass.new
k.__send__(:hello, "gentle", "readers")  #=> "Hello gentle readers"


201
202
203
# File 'lib/source/ruby.rb', line 201

def __send__(method,*args)
  `this[method].apply(this,args)`
end

#classObject

call-seq:

obj.class -> class

Returns the class of obj. This method must always be called with an explicit receiver, as class is also a reserved word.

'a'.class     #=> String
self.class    #=> Object


214
215
216
# File 'lib/source/ruby.rb', line 214

def class
  return `c$Object`
end

#cloneObject

call-seq:

obj.clone -> object

Produces a shallow copy of obj – the instance variables of obj are copied, but not the objects they reference. See also the discussion under Object#dup.

class Klass
   attr_accessor :str
end

s1 = Klass.new      #=> #<Klass:100>
s1.str = "Hello"    #=> "Hello"
s2 = s1.clone       #=> #<Klass:101>
s2.str[1,4] = "i"   #=> "i"
s1.str              #=> "Hi"
s2.str              #=> "Hi"

This method may have class-specific behavior. If so, that behavior will be documented under the initialize_copy method of the class.



239
240
241
242
243
244
# File 'lib/source/ruby.rb', line 239

def clone
  `var result={}`
  `for(var x in this){if(x!='_objectId'){result[x]=this[x];};}`
  `result._objectId=$objectId++`
  return `result`
end

#dupObject

call-seq:

obj.dup -> object

Produces a shallow copy of obj – the instance variables of obj are copied, but not the objects they reference. See also the discussion under Object#clone. In general, clone and dup may have different semantics in descendent classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the new instance.

This method may have class-specific behavior. If so, that behavior will be documented under the initialize_copy method of the class.



259
260
261
262
263
264
# File 'lib/source/ruby.rb', line 259

def dup
  `var result=this.m$class.m$new()`
  `for(var x in this){if(x!='_objectId'&&x.slice(0,2)!='i$'){result[x]=this[x];};}`
  `result._objectId=$objectId++`
  return `result`
end

#enumObject

FIX: Incomplete



267
268
# File 'lib/source/ruby.rb', line 267

def enum
end

#eql?(other) ⇒ Boolean

call-seq:

obj == other      -> true or false
obj.eql?(other)   -> true or false
obj.equal?(other) -> true or false

Equality – at the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

The eql? method returns true if obj and other have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses may override this behavior.

Returns:

  • (Boolean)


288
289
290
# File 'lib/source/ruby.rb', line 288

def eql?(other)
  `this._objectId==other._objectId`
end

#equal?(other) ⇒ Boolean

call-seq:

obj == other      -> true or false
obj.eql?(other)   -> true or false
obj.equal?(other) -> true or false

Equality – at the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendent classes to provide class-specific meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

The eql? method returns true if obj and other have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses may override this behavior.

Returns:

  • (Boolean)


310
311
312
# File 'lib/source/ruby.rb', line 310

def equal?(other)
  `this._objectId==other._objectId`
end

#extend(*modules) ⇒ Object

call-seq:

obj.extend(module, ...) -> obj

Adds to obj the instance methods from each module given as a parameter. Internally, invokes extend_object and the callback extended on each given module in turn, passing obj as the argument.

module Mod
  def hello
    "Hello from Mod"
  end
end

class Klass
  def hello
    "Hello from Klass"
  end
end

k = Klass.new
k.hello         #=> "Hello from Klass"
k.extend(Mod)   #=> #<Klass:100>
k.hello         #=> "Hello from Mod"


339
340
341
342
# File 'lib/source/ruby.rb', line 339

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

#hashObject

call-seq:

obj.hash -> js_string

Generates a hash value for this object, in JavaScript native string form, which is used by class Hash to access its internal contents table. This function must have the property that a.eql?(b) implies a.hash == b.hash, and is typically overridden in child classes.



352
353
354
# File 'lib/source/ruby.rb', line 352

def hash
  `'o_'+this._objectId`
end

#inspectObject

call-seq:

obj.inspect -> string

Returns a string containing a human-readable representation of obj. If not overridden, uses the to_s method to generate the string.



362
363
364
# File 'lib/source/ruby.rb', line 362

def inspect
  `this.m$toS()`
end

#instance_eval(&block) ⇒ Object

call-seq:

obj.instance_eval { || block } -> object

Evaluates the given block within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables.

class Klass
  def initialize
    @secret = 99
  end
end

k = Klass.new
k.instance_eval { @secret }   #=> 99


382
383
384
# File 'lib/source/ruby.rb', line 382

def instance_eval(&block)
  `block._block.m$(this)()`
end

#instance_of?(klass) ⇒ Boolean

call-seq:

obj.instance_of?(class) -> true or false

Returns true if obj is an instance of the given class. See also Object#kind_of?.

Returns:

  • (Boolean)


392
393
394
# File 'lib/source/ruby.rb', line 392

def instance_of?(klass)
  `this.m$class()==klass`
end

#instance_variable_defined?(name) ⇒ Boolean

call-seq:

obj.instance_variable_defined?(sym) -> true or false

Returns true if the given instance variable is defined in obj.

class Klass
  def initialize(a)
    @a = a
  end
end

k = Klass.new(99)
k.instance_variable_defined?(:@a)    #=> true
k.instance_variable_defined?("@b")   #=> false

Returns:

  • (Boolean)


411
412
413
# File 'lib/source/ruby.rb', line 411

def instance_variable_defined?(name)
  `this[name._value.replace('@','i$')]!=null`
end

#instance_variable_get(name) ⇒ Object

call-seq:

obj.instance_variable_get(sym) -> object

Returns the value of the given instance variable, or nil if the instance variable is not set. The @ part of the variable name should be included for regular instance variables.

class Klass
  def initialize(a)
    @a = a
  end
end

k = Klass.new(99)
k.instance_variable_get(:@a)    #=> 99


431
432
433
434
# File 'lib/source/ruby.rb', line 431

def instance_variable_get(name)
  `var v=this[name._value.replace('@','i$')]`
  `v==null?nil:v`
end

#instance_variable_set(name, obj) ⇒ Object

call-seq:

obj.instance_variable_set(sym, object) -> object

Sets the instance variable named by sym to obj. The variable need not exist prior to this call.

class Klass
  def initialize(a)
    @a = a
  end
end

k = Klass.new(99)
k.instance_variable_set(:@a,79)   #=> 79
k.instance_variable_get('@a')     #=> 79

FIX: Incomplete



453
454
455
# File 'lib/source/ruby.rb', line 453

def instance_variable_set(name, obj)
  `this[name._value.replace('@','i$')]=obj`
end

#instance_variablesObject

call-seq:

obj.instance_variables -> array

Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.

class Klass
  attr_accessor :iv
  def initialize
    @v = 5
  end
end

k = Klass.new
k.instance_variables    #=> ["@v"]


474
475
476
477
478
# File 'lib/source/ruby.rb', line 474

def instance_variables
  `var result=[]`
  `for(var x in this){if(x.slice(0,2)=='i$'){result.push($q('@'+$_uncamel(x.slice(2,x.length))));};}`
  return `result`
end

#is_a?(klass) ⇒ Boolean

call-seq:

obj.is_a?(class)    -> true or false
obj.kind_of?(class) -> true or false

Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

module M; end
class A
  include M
end
class B < A; end
class C < B; end
b = B.new
b.instance_of? A    #=> false
b.instance_of? B    #=> true
b.instance_of? C    #=> false
b.instance_of? M    #=> false
b.is_a? A           #=> true
b.is_a? B           #=> true
b.is_a? C           #=> false
b.is_a? M           #=> true

FIX: Incomplete

Returns:

  • (Boolean)


504
505
506
507
508
509
510
511
# File 'lib/source/ruby.rb', line 504

def is_a?(klass)
  `if(this.m$class()==klass||c$Object==klass){return true;}`  # true if instance_of? or if klass is Object
  `if(this.m$class()._modules[klass]){return true;}`            # true if klass is included in obj's class
  `if(this.m$class()==c$Object){return false;}`               # false if module check fails and obj is Object
  `var bubble=this.m$class(),result=false`
  `while(bubble!=c$Object){if(klass==bubble||bubble._modules[klass]!=null){result=true;};if(result){break;};bubble=bubble._superclass;}`
  return `result`
end

#kind_of?(klass) ⇒ Boolean

call-seq:

obj.is_a?(class)    -> true or false
obj.kind_of?(class) -> true or false

Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

module M; end
class A
  include M
end
class B < A; end
class C < B; end
b = B.new
b.instance_of? A    #=> false
b.instance_of? B    #=> true
b.instance_of? C    #=> false
b.instance_of? M    #=> false
b.kind_of? A        #=> true
b.kind_of? B        #=> true
b.kind_of? C        #=> false
b.kind_of? M        #=> true

FIX: Incomplete

Returns:

  • (Boolean)


537
538
539
540
541
542
543
544
# File 'lib/source/ruby.rb', line 537

def kind_of?(klass)
  `if(this.m$class()==klass||c$Object==klass){return true;}`  # true if instance_of? or if klass is Object
  `if(this.m$class()._modules[klass]){return true;}`            # true if klass is included in obj's class
  `if(this.m$class()==c$Object){return false;}`               # false if module check fails and obj is Object
  `var bubble=this.m$class(),result=false`
  `while(bubble!=c$Object){if(klass==bubble||bubble._modules[klass]!=null){result=true;};if(result){break;};bubble=bubble._superclass;}`
  return `result`
end

#method(name) ⇒ Object

call-seq:

obj.method(sym) -> method

Looks up the named method as a receiver in obj, returning a Method object (or raising NameError). The Method object acts as a closure in obj’s object instance, so instance variables and the value of self remain available.

class Klass
  def initialize(n)
    @iv = n
  end

  def hello
    "Hello, @iv = #{@iv}"
  end
end

k1 = Klass.new(4)
m1 = k1.method(:hello)
m1.call   #=> "Hello, @iv = 4"

k2 = Klass.new('four')
m2 = k2.method("hello")
m2.call   #=> "Hello, @iv = four"

FIX: Incomplete



573
574
# File 'lib/source/ruby.rb', line 573

def method(name)
end

#methodsObject

call-seq:

obj.methods -> array

Returns a list of the names of methods publicly accessible in obj. This will include all the methods accessible in obj’s ancestors.

class Klass
  def k_method
  end
end

k = Klass.new
k.methods[0..3]    #=> ["k_method", "nil?", "is_a?", "class"]
k.methods.length   #=> 42


591
592
593
594
595
# File 'lib/source/ruby.rb', line 591

def methods
  `var result=[]`
  `for(var x in this){if(x.slice(0,2)=='m$'&&x!='m$initialize'){$q($_uncamel(x.slice(2,x.length)));};}`
  return `result`
end

#nil?Boolean

call-seq:

nil.nil? -> true
obj.nil? -> false

Only the object nil responds true to nil?.

Returns:

  • (Boolean)


603
604
605
# File 'lib/source/ruby.rb', line 603

def nil?
  false
end

#object_idObject

call-seq:

obj.__id__    -> integer
obj.object_id -> integer

Returns an integer identifier for obj. The same number will be returned on all calls to object_id for a given object, and no two active objects will share an id.



615
616
617
# File 'lib/source/ruby.rb', line 615

def object_id
  `this._objectId`
end

#remove_instance_variable(sym) ⇒ Object

call-seq:

obj.remove_instance_variable(sym) -> obj

Removes the named instance variable from obj, returning that variable’s value.

class Klass
  attr_reader :var
  def initialize
    @var = 99
  end
end

k = Klass.new
k.var                               #=> 99
k.remove_instance_variable(:@var)   #=> 99
k.var                               #=> nil

FIX: Incomplete



638
639
# File 'lib/source/ruby.rb', line 638

def remove_instance_variable(sym)
end

#respond_to?(method) ⇒ Boolean

call-seq:

obj.respond_to?(sym) -> true or false

Returns true if obj responds to the given method.

Returns:

  • (Boolean)


646
647
648
# File 'lib/source/ruby.rb', line 646

def respond_to?(method)
  `typeof this[method]=='function'`
end

#send(method, *args) ⇒ Object

call-seq:

obj.__send__(sym [, args...]) -> obj
obj.send(sym [, args...])     -> obj

Invokes the method identified by sym, passing it any arguments specified. Use __send__ if the name send clashes with an existing method in obj.

class Klass
  def hello(*args)
    "Hello " + args.join(' ')
  end
end

k = Klass.new
k.send(:hello, "gentle", "readers")  #=> "Hello gentle readers"


667
668
669
# File 'lib/source/ruby.rb', line 667

def send(method,*args)
  `this[method].apply(this,args)`
end

#singleton_method_addedObject

FIX: Incomplete



672
673
# File 'lib/source/ruby.rb', line 672

def singleton_method_added
end

#singleton_method_removedObject

FIX: Incomplete



676
677
# File 'lib/source/ruby.rb', line 676

def singleton_method_removed
end

#singleton_method_undefinedObject

FIX: Incomplete



680
681
# File 'lib/source/ruby.rb', line 680

def singleton_method_undefined
end

#singleton_methodsObject

FIX: Incomplete



684
685
# File 'lib/source/ruby.rb', line 684

def singleton_methods
end

#to_enumObject

FIX: Incomplete



688
689
# File 'lib/source/ruby.rb', line 688

def to_enum
end

#to_sObject

call-seq:

obj.to_s -> string

Returns a string representing obj. The default to_s prints obj’s class and a version of obj’s object_id spoofed to resemble Ruby’s 6-digit hex memory representation.



698
699
700
# File 'lib/source/ruby.rb', line 698

def to_s
  `$q('#<'+this.m$class()._name+':0x'+(this._objectId*999^4000000).toString(16)+'>')`
end