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:



150
151
# File 'lib/source/ruby.rb', line 150

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.



171
172
173
# File 'lib/source/ruby.rb', line 171

def ==(other)
  `this.__id__==other.__id__`
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.



182
183
184
# File 'lib/source/ruby.rb', line 182

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

#=~Object

call-seq:

obj =~ other -> false

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



192
193
194
# File 'lib/source/ruby.rb', line 192

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.



204
205
206
# File 'lib/source/ruby.rb', line 204

def __id__
  `this.__id__`
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"


225
226
227
# File 'lib/source/ruby.rb', line 225

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


238
239
240
# File 'lib/source/ruby.rb', line 238

def class
  `this.__class__`
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.



263
264
265
266
267
268
# File 'lib/source/ruby.rb', line 263

def clone
  `var result={}`
  `for(var x in this){if(x!='__id__'){result[x]=this[x];};}`
  `result.__id__=Red.id++`
  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.



283
284
285
286
287
288
# File 'lib/source/ruby.rb', line 283

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

#enumObject

FIX: Incomplete



291
292
# File 'lib/source/ruby.rb', line 291

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)


312
313
314
# File 'lib/source/ruby.rb', line 312

def eql?(other)
  `this.__id__==other.__id__`
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)


334
335
336
# File 'lib/source/ruby.rb', line 334

def equal?(other)
  `this.__id__==other.__id__`
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"


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

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.



376
377
378
# File 'lib/source/ruby.rb', line 376

def hash
  `'o_'+this.__id__`
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.



386
387
388
# File 'lib/source/ruby.rb', line 386

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


406
407
408
# File 'lib/source/ruby.rb', line 406

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)


416
417
418
# File 'lib/source/ruby.rb', line 416

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)


435
436
437
# File 'lib/source/ruby.rb', line 435

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


455
456
457
458
# File 'lib/source/ruby.rb', line 455

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



477
478
479
# File 'lib/source/ruby.rb', line 477

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"]


498
499
500
501
502
# File 'lib/source/ruby.rb', line 498

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)


528
529
530
531
532
533
534
535
# File 'lib/source/ruby.rb', line 528

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)


561
562
563
564
565
566
567
568
# File 'lib/source/ruby.rb', line 561

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



597
598
# File 'lib/source/ruby.rb', line 597

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


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

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)


627
628
629
# File 'lib/source/ruby.rb', line 627

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.



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

def object_id
  `this.__id__`
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



662
663
# File 'lib/source/ruby.rb', line 662

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)


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

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"


691
692
693
# File 'lib/source/ruby.rb', line 691

def send(method,*args)
  `this['m$'+method._value.replace('=','Eql')].apply(this,args)`
end

#singleton_method_addedObject

FIX: Incomplete



696
697
# File 'lib/source/ruby.rb', line 696

def singleton_method_added
end

#singleton_method_removedObject

FIX: Incomplete



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

def singleton_method_removed
end

#singleton_method_undefinedObject

FIX: Incomplete



704
705
# File 'lib/source/ruby.rb', line 704

def singleton_method_undefined
end

#singleton_methodsObject

FIX: Incomplete



708
709
# File 'lib/source/ruby.rb', line 708

def singleton_methods
end

#to_enumObject

FIX: Incomplete



712
713
# File 'lib/source/ruby.rb', line 712

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.



722
723
724
# File 'lib/source/ruby.rb', line 722

def to_s
  `$q('#<'+this.m$class().__name__.replace(/\\./g,'::')+':0x'+(this.__id__*999^4000000).toString(16)+'>')`
end