ruby 符号(语法)可以包含什么? [英] What can a ruby symbol (syntax) contain?

查看:36
本文介绍了ruby 符号(语法)可以包含什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建正则表达式来匹配 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}"
  • 单引号 ArraySymbol 字面量:在 String 的单引号 Array 中有效的所有内容>s 文字,例如%i|foo bar baz| (相当于[:foo, :bar, :baz]), %i(foo\ bar baz)(相当于[:'foo bar', :baz]),%i:foo bar:(相当于[:foo, :bar])
  • 双引号 ArraySymbol 字面量:在 StringArray 中有效的所有内容>s 文字,例如%I|foo #{bar} baz|
  • Symbol key: value 语法中的散列键:每个有效的 Ruby 标签,例如{foo: 42}
  • Symbol 引用的 'key': value 语法中的哈希键:每个有效的 Ruby String 文字,包括转义序列和插值,例如{"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-quoted String literal, including escape sequences such as :'\'' and :'\\'
  • double-quoted : literal: everything that's valid in a double-quoted String 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 the Symbol, e.g. :"#{if rand < 0.5 then RUBY_VERSION else ENV['HOME'] end}"
  • single-quoted Array of Symbols literal: everything that's valid in a single-quoted Array of Strings 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 of Symbols literal: everything that's valid in a double-quoted Array of Strings literal, e.g. %I|foo #{bar} baz|, etc.
  • Symbol hash keys in the key: value syntax: every valid Ruby label, e.g. {foo: 42}
  • Symbol hash keys in the quoted 'key': value syntax: every valid Ruby String literal, including escape sequences and interpolation, e.g. {"foo\n#{bar}": 42}

当然还有很多其他表达式的计算结果为 Symbols:

There are of course a lot of other expressions that evaluate to Symbols:

  • 方法定义表达式: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 (alias String#intern): 'foo bar'.to_sym # => :'foo bar'
  • really, any method that may return a Symbol

这篇关于ruby 符号(语法)可以包含什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆