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:



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

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.



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

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.



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

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.



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

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.



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

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"


226
227
228
229
230
# File 'lib/source/ruby.rb', line 226

def __send__(method,*args)
  `method=this['m$'+sym._value.replace('=','Eql')]`
  `if(!method){m$raise(c$NoMethodError,$q('undefined method "'+sym._value+'" for '+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


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

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.



266
267
268
269
270
271
# File 'lib/source/ruby.rb', line 266

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.



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

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



294
295
# File 'lib/source/ruby.rb', line 294

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)


315
316
317
# File 'lib/source/ruby.rb', line 315

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)


337
338
339
# File 'lib/source/ruby.rb', line 337

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"


366
367
368
369
# File 'lib/source/ruby.rb', line 366

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.



379
380
381
# File 'lib/source/ruby.rb', line 379

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.



389
390
391
# File 'lib/source/ruby.rb', line 389

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


409
410
411
# File 'lib/source/ruby.rb', line 409

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)


419
420
421
# File 'lib/source/ruby.rb', line 419

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)


438
439
440
# File 'lib/source/ruby.rb', line 438

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


458
459
460
461
# File 'lib/source/ruby.rb', line 458

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



480
481
482
# File 'lib/source/ruby.rb', line 480

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


501
502
503
504
505
# File 'lib/source/ruby.rb', line 501

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)


531
532
533
534
535
536
537
538
# File 'lib/source/ruby.rb', line 531

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)


564
565
566
567
568
569
570
571
# File 'lib/source/ruby.rb', line 564

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



600
601
# File 'lib/source/ruby.rb', line 600

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


618
619
620
621
622
# File 'lib/source/ruby.rb', line 618

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)


630
631
632
# File 'lib/source/ruby.rb', line 630

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.



642
643
644
# File 'lib/source/ruby.rb', line 642

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



665
666
# File 'lib/source/ruby.rb', line 665

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)


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

def respond_to?(method)
  `typeof(this['m$'+method._value])=='function'`
end

#send(sym, *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"


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

def send(sym,*args)
  `method=this['m$'+sym._value.replace('=','Eql')]`
  `if(!method){m$raise(c$NoMethodError,$q('undefined method "'+sym._value+'" for '+this));}`
  `method.apply(this,args)`
end

#singleton_method_addedObject

FIX: Incomplete



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

def singleton_method_added
end

#singleton_method_removedObject

FIX: Incomplete



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

def singleton_method_removed
end

#singleton_method_undefinedObject

FIX: Incomplete



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

def singleton_method_undefined
end

#singleton_methodsObject

FIX: Incomplete



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

def singleton_methods
end

#to_enumObject

FIX: Incomplete



717
718
# File 'lib/source/ruby.rb', line 717

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.



727
728
729
# File 'lib/source/ruby.rb', line 727

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