在Ruby符号的上下文中,"taguri"是什么意思?

Jos*_*ver 2 ruby

我和我的一位同事试图理解为什么在对它们执行正则表达式匹配时,符号不会自动进行字符串化:

>> :this =~ /./
=> false
>> :this =~ :this
=> false
>> :this =~ /:this/
=> false
Run Code Online (Sandbox Code Playgroud)

一种理论是Symbol覆盖了:=〜方法,所以我们检查了:this.methods.我们发现Symbol不会覆盖:=〜(1),但也注意到一种非常奇怪的方法:

>> :this.respond_to? :taguri=
=> true
Run Code Online (Sandbox Code Playgroud)

在日本,たぐり(taguri)的意思是"缫丝(线头等)"(2),但我不能为我的生命搞清楚什么是有一个符号做了,我找不到红宝石Symbol类中方法的源代码.

有线索吗?

小智 5

它不是"taguri",而是"Tag URI".查看源代码,它似乎都处理YAML,如果你查看YAML文档,你会看到:http://ruby-doc.org/ruby-1.9/classes/YAML.html

这是来自tag.rb的绝对证明:

# Associates a taguri _tag_ with a Ruby class _cls_.  The taguri is used to give types
# to classes when loading YAML.  Taguris are of the form:
#
#   tag:authorityName,date:specific
#
# The +authorityName+ is a domain name or email address.  The +date+ is the date the type
# was issued in YYYY or YYYY-MM or YYYY-MM-DD format.  The +specific+ is a name for
# the type being added.
#
# For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
# +date+.  The +specific+ is simply the name of the type:
#
#  tag:yaml.org,2002:int
#  tag:yaml.org,2002:float
#  tag:yaml.org,2002:timestamp
#
# The domain must be owned by you on the +date+ declared.  If you don't own any domains on the
# date you declare the type, you can simply use an e-mail address.
#
#  tag:why@ruby-lang.org,2004:notes/personal
#
Run Code Online (Sandbox Code Playgroud)