Ruby/Справочник/Symbol

Класс SymbolПравить

Symbol objects represent names and some strings inside the Ruby 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 a program's execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the Symbol :Fred will be the same object in all three contexts.

  module One
    class Fred
    end
    $f1 = :Fred
  end
  module Two
    Fred = 1
    $f2 = :Fred
  end
  def Fred()
  end
  $f3 = :Fred
  $f1.id   #=> 2514190
  $f2.id   #=> 2514190
  $f3.id   #=> 2514190

Методы класса

all_symbols, yaml_new

Методы объекта

===, dclone, id2name, inspect, to_int, to_i, to_proc, to_sym, to_s, to_yaml

Symbol::all_symbolsПравить


 Symbol.all_symbols    => array

Returns an array of all the symbols currently in Ruby's symbol table.

  Symbol.all_symbols.size    #=> 903
  Symbol.all_symbols[1,20]   #=> [:floor, :ARGV, :Binding, :symlink,
                                  :chown, :EOFError, :$;, :String,
                                  :LOCK_SH, :"setuid?", :$<,
                                  :default_proc, :compact, :extend,
                                  :Tms, :getwd, :$=, :ThreadGroup,
                                  :wait2, :$>]


Symbol#===Править


 obj == other        => true or false
 obj.equal?(other)   => true or false
 obj.eql?(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 anObject have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses normally continue this tradition, but there are exceptions. Numeric types, for example, perform type conversion across ==, but not across eql?, so:

  1 == 1.0     #=> true
  1.eql? 1.0   #=> false

Symbol#dcloneПравить


 dclone()

(нет описания...)

Symbol#id2nameПравить


 sym.id2name   => string
 sym.to_s      => string

Returns the name or string corresponding to sym.

  :fred.id2name   #=> "fred"

Symbol#inspectПравить


 sym.inspect    => string

Returns the representation of sym as a symbol literal.

  :fred.inspect   #=> ":fred"

Symbol#to_iПравить


 sym.to_i      => fixnum

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

  :fred.to_i           #=> 9809
  "fred".to_sym.to_i   #=> 9809

Symbol#to_intПравить


 to_int()

nodoc:

Symbol#to_procПравить


 to_proc()

Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:

 # The same as people.collect { |p| p.name }
 people.collect(&:name)
 # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
 people.select(&:manager?).collect(&:salary)

Symbol#to_sПравить


 sym.id2name   => string
 sym.to_s      => string

Returns the name or string corresponding to sym.

  :fred.id2name   #=> "fred"

Symbol#to_symПравить


 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.