Class: Symbol

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

Symbol objects represent names and some strings inside the Red interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods. The same Symbol object will be created for a given name or string for the duration of an application’s execution, regardless of the context or meaning of that name. Thus if Foo is a constant in one context, a method in another, and a class in a third, the Symbol :Foo will be the same object in all three contexts.

module One
  class Foo
  end
  $f1 = :Foo
end

module Two
  Foo = 1
  $f2 = :Foo
end

def Foo
end
$f3 = :Foo

$f1.object_id   #=> 4190
$f2.object_id   #=> 4190
$f3.object_id   #=> 4190

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Symbol

:nodoc:



6190
6191
6192
6193
# File 'lib/source/ruby.rb', line 6190

def initialize(value) # :nodoc:
  `this._value=value`
  `c$Symbol._table[value]=this`
end

Class Method Details

.all_symbolsObject

call-seq:

Symbol.all_symbols -> array

Returns an array of all the symbols currently in Red’s symbol table.



6184
6185
6186
6187
6188
# File 'lib/source/ruby.rb', line 6184

def self.all_symbols
  `var result=[]`
  `for(var x in c$Symbol._table){if(c$Symbol._table[x].m$class()==c$Symbol){result.push(c$Symbol._table[x]);}}`
  return `result`
end

Instance Method Details

#hashObject

:nodoc:



6195
6196
6197
# File 'lib/source/ruby.rb', line 6195

def hash # :nodoc:
  `'s_'+this._value`
end

#id2nameObject

call-seq:

sym.id2name -> string
sym.to_s    -> string

Returns the name or string corresponding to sym.

:foo.id2name    #=> "foo"


6207
6208
6209
# File 'lib/source/ruby.rb', line 6207

def id2name
  `$q(this._value)`
end

#inspectObject

call-seq:

sym.inspect -> string

Returns the representation of sym as a symbol literal.

:foo.inspect    #=> ":foo"


6218
6219
6220
# File 'lib/source/ruby.rb', line 6218

def inspect
  `$q(''+this)`
end

#to_iObject

call-seq:

sym.to_i -> fixnum

Returns an integer that is unique for each symbol within a particular execution of a program.

:foo.to_i   #=> 2019


6230
6231
6232
# File 'lib/source/ruby.rb', line 6230

def to_i
  `this.__id__`
end

#to_sObject

call-seq:

sym.id2name -> string
sym.to_s    -> string

Returns the name or string corresponding to sym.

:foo.to_s   #=> "foo"


6242
6243
6244
# File 'lib/source/ruby.rb', line 6242

def to_s
  `$q(this._value)`
end

#to_symObject

call-seq:

sym.to_sym -> sym

In general, to_sym returns the Symbol corresponding to an object. As sym is already a symbol, self is returned in this case.



6252
6253
6254
# File 'lib/source/ruby.rb', line 6252

def to_sym
  return self
end