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:



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

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.



6222
6223
6224
6225
6226
# File 'lib/source/ruby.rb', line 6222

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:



6233
6234
6235
# File 'lib/source/ruby.rb', line 6233

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"


6245
6246
6247
# File 'lib/source/ruby.rb', line 6245

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

#inspectObject

call-seq:

sym.inspect -> string

Returns the representation of sym as a symbol literal.

:foo.inspect    #=> ":foo"


6256
6257
6258
# File 'lib/source/ruby.rb', line 6256

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


6268
6269
6270
# File 'lib/source/ruby.rb', line 6268

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"


6280
6281
6282
# File 'lib/source/ruby.rb', line 6280

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.



6290
6291
6292
# File 'lib/source/ruby.rb', line 6290

def to_sym
  return self
end