What can a ruby symbol (syntax) contain?

2019-02-27 02:49发布

问题:

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

回答1:

It's not entirely clear what you are talking about.

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.

If you are talking about the various ways of writing Symbol literals, here's my best understanding:

  • 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}

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

  • 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


回答2:

http://www.cse.buffalo.edu/~regan/cse305/RubyBNF.pdf enumerates the context-free grammar productions that define Ruby's syntax. CFGs are inherently more powerful than REs, so you might want to consider a different tool for this job--but you can certainly look at this document and try to construct a regexp that matches all cases.



标签: ruby symbols