ruby 符号(语法)可以包含什么? [英] What can a ruby symbol (syntax) contain?
问题描述
我想创建正则表达式来匹配 ruby 符号,但我需要知道符号的确切语法是什么.
I want to create regular expression to match ruby symbols, but I need to know what the exact syntax for a symbol is.
到目前为止,我知道以下几点:
Until now I am aware of the following:
:'string'
:"string"
:__underline
:method
:exclamation!
:question?
:@instance
:$global
推荐答案
你在说什么不是很清楚.
It's not entirely clear what you are talking about.
如果您在谈论 Symbol
可以包含什么,答案是:任何东西,包括换行符、任意空白、控制字符、任意奇怪和晦涩难懂的 Unicode 字符,以及其他所有内容.
If you are talking about what a Symbol
can contain, the answer is: anything and everything, including newlines, arbitrary whitespace, control characters, arbitrarily weird and obscure Unicode characters, and everything else.
如果您在谈论编写 Symbol
文字的各种方式,以下是我最好的理解:
If you are talking about the various ways of writing Symbol
literals, here's my best understanding:
- bare
:
文字:任何有效的 Ruby 标识符(例如:foo
、:Foo
、:@foo
,:@@foo
,:$foo
,:$:
, ...) - 单引号
:
文字:在单引号String
文字中有效的所有内容,包括转义序列,例如:'\''
和:'\\'
- 双引号
:
字面量:双引号String
字面量中有效的所有内容,包括转义序列,例如:"\""
、:"\\"
和:"\n"
,以及字符串插值,它允许您将任意 Ruby 代码的结果注入符号
,例如:"#{if rand <;0.5 然后 RUBY_VERSION else ENV['HOME'] end}"
- 单引号
Array
的Symbol
字面量:在String
的单引号Array
中有效的所有内容>s 文字,例如%i|foo bar baz|
(相当于[:foo, :bar, :baz]
),%i(foo\ bar baz)
(相当于[:'foo bar', :baz]
),%i:foo bar:
(相当于[:foo, :bar]
) - 双引号
Array
的Symbol
字面量:在String
Array 中有效的所有内容>s 文字,例如%I|foo #{bar} baz|
等 Symbol
key: value
语法中的散列键:每个有效的 Ruby 标签,例如{foo: 42}
Symbol
引用的'key': value
语法中的哈希键:每个有效的 RubyString
文字,包括转义序列和插值,例如{"foo\n#{bar}": 42}
- bare
:
literal: any valid Ruby identifier (e.g.:foo
,:Foo
,:@foo
,:@@foo
,:$foo
,:$:
, …) - single-quoted
:
literal: everything that's valid in a single-quotedString
literal, including escape sequences such as:'\''
and:'\\'
- double-quoted
:
literal: everything that's valid in a double-quotedString
literal, including escape sequences such as:"\""
,:"\\"
, and:"\n"
, as well as string interpolation, which allows you to inject the results of arbitrary Ruby code into theSymbol
, e.g.:"#{if rand < 0.5 then RUBY_VERSION else ENV['HOME'] end}"
- single-quoted
Array
ofSymbol
s literal: everything that's valid in a single-quotedArray
ofString
s literal, e.g.%i|foo bar baz|
(equivalent to[:foo, :bar, :baz]
),%i(foo\ bar baz)
(equivalent to[:'foo bar', :baz]
),%i:foo bar:
(equivalent to[:foo, :bar]
) - double-quoted
Array
ofSymbol
s literal: everything that's valid in a double-quotedArray
ofString
s literal, e.g.%I|foo #{bar} baz|
, etc. Symbol
hash keys in thekey: value
syntax: every valid Ruby label, e.g.{foo: 42}
Symbol
hash keys in the quoted'key': value
syntax: every valid RubyString
literal, including escape sequences and interpolation, e.g.{"foo\n#{bar}": 42}
当然还有很多其他表达式的计算结果为 Symbol
s:
There are of course a lot of other expressions that evaluate to Symbol
s:
- 方法定义表达式:
def foo;end # =>:foo
String#to_sym
(别名String#intern
):'foo bar'.to_sym # =>:'foo bar'
- 真的,任何方法可以
返回
一个Symbol
- method definition expressions:
def foo;end # => :foo
String#to_sym
(aliasString#intern
):'foo bar'.to_sym # => :'foo bar'
- really, any method that may
return
aSymbol
这篇关于ruby 符号(语法)可以包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!