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:



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

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.



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

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.



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

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.



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

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.



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

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"


234
235
236
237
238
# File 'lib/source/ruby.rb', line 234

def __send__(method,*args)
  `method=this['m$'+sym.__value__.replace('=','_eql').replace('!','_bang').replace('?','_bool')]`
  `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


249
250
251
# File 'lib/source/ruby.rb', line 249

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.



274
275
276
277
278
279
# File 'lib/source/ruby.rb', line 274

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.



294
295
296
297
298
299
# File 'lib/source/ruby.rb', line 294

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



302
303
# File 'lib/source/ruby.rb', line 302

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)


323
324
325
# File 'lib/source/ruby.rb', line 323

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)


345
346
347
# File 'lib/source/ruby.rb', line 345

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"


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

def extend(*modules)
  `for(var i=0,l=modules.length;i<l;++i){modules[i].m$extend_object(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.



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

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.



397
398
399
# File 'lib/source/ruby.rb', line 397

def inspect
  `this.m$to_s()`
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


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

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)


427
428
429
# File 'lib/source/ruby.rb', line 427

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)


446
447
448
# File 'lib/source/ruby.rb', line 446

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


466
467
468
469
# File 'lib/source/ruby.rb', line 466

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



488
489
490
# File 'lib/source/ruby.rb', line 488

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


509
510
511
512
513
# File 'lib/source/ruby.rb', line 509

def instance_variables
  `var result=[]`
  `for(var x in this){if(x.slice(0,2)=='i$'){result.push($q('@'+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)


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

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)


572
573
574
575
576
577
578
579
# File 'lib/source/ruby.rb', line 572

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



608
609
# File 'lib/source/ruby.rb', line 608

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


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

def methods
  `var result=[],sub={_eql2:'==',_eql3:'===',_etld:'=~',_brac:'[]',_breq:'[]=',_lteq:'<=',_gteq:'>=',_ltlt:'<<',_gtgt:'>>',_lthn:'<',_gthn:'>',_ltgt:'<=>',_pipe:'|',_ampe:'&',_plus:'+',_posi:'+@',_nega:'-@',_star:'*',_str2:'**',_slsh:'/',_perc:'%',_care:'^',_tild:'~'}`
  `for(var x in this){if(x.slice(0,2)=='m$'&&x!='m$initialize'){var str=x.slice(2,x.length);str=sub[str]||str;result.push($q(str.replace('_bool','?').replace('_bang','!').replace('_eql','=')));};}`
  return `result`
end

#nil?Boolean

call-seq:

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

Only the object nil responds true to nil?.

Returns:

  • (Boolean)


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

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.



650
651
652
# File 'lib/source/ruby.rb', line 650

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



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

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)


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

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"


702
703
704
705
706
707
# File 'lib/source/ruby.rb', line 702

def send(sym,*args)
  `var str=sym.__value__.replace('=','_eql').replace('!','_bang').replace('?','_bool');sub={'==':'_eql2','===':'_eql3','=~':'_etld','[]':'_brac','[]=':'_breq','<=':'_lteq','>=':'_gteq','<<':'_ltlt','>>':'_gtgt','<':'_lthn','>':'_gthn','<=>':'_ltgt','|':'_pipe','&':'_ampe','+':'_plus','+@':'_posi','-@':'_nega','*':'_star','**':'_str2','/':'_slsh','%':'_perc','^':'_care','~':'_tild'}`
  `var method=this['m$'+(sub[str]||str)]`
  `if(!method){m$raise(c$NoMethodError,$q('undefined method "'+sym.__value__+'" for '+this));}`
  `method.apply(this,args)`
end

#singleton_method_addedObject

FIX: Incomplete



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

def singleton_method_added
end

#singleton_method_removedObject

FIX: Incomplete



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

def singleton_method_removed
end

#singleton_method_undefinedObject

FIX: Incomplete



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

def singleton_method_undefined
end

#singleton_methodsObject

FIX: Incomplete



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

def singleton_methods
end

#to_enumObject

FIX: Incomplete



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

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.



736
737
738
# File 'lib/source/ruby.rb', line 736

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