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:



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

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.



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

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.



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

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.



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

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.



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

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"


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

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


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

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.



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

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.



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

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



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

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)


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

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)


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

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"


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

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.



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

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.



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

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


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

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)


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

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)


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

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


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

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



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

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


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

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)


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

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)


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

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



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

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


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

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)


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

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.



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

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



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

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)


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

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"


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

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



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

def singleton_method_added
end

#singleton_method_removedObject

FIX: Incomplete



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

def singleton_method_removed
end

#singleton_method_undefinedObject

FIX: Incomplete



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

def singleton_method_undefined
end

#singleton_methodsObject

FIX: Incomplete



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

def singleton_methods
end

#to_enumObject

FIX: Incomplete



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

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.



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

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