Project1

标题: 请问":"是什么玩意 [打印本页]

作者: 冷俊逸    时间: 2016-4-9 08:39
标题: 请问":"是什么玩意
一开始还以为是取消应用序,但是如果我引用:函数的话,输出的就是这个函数的名字了。
作者: taroxd    时间: 2016-4-10 07:09
本帖最后由 taroxd 于 2016-4-10 07:15 编辑

Quoted from: http://ruby-doc.org/core-2.3.0/Symbol.html

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.object_id   #=> 2514190
$f2.object_id   #=> 2514190
$f3.object_id   #=> 2514190





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1