Like khelwood said in the comments symbols with or without quotes are identical.
:foo == :"foo" #=> true
The reason Ruby displays some symbols with and other symbols without quotes is due to their content. Symbols that are conform the standards of methods names will be displayed without quotes, while symbols that don't conform to the format are displayed with quotes.
Meaning that:
# operators are displayed without quotes
:">>" #=> :>>
# snake case naming will be displayed without quotes
:"foo_bar" #=> :foo_bar
# symbols starting with a number will be displayed with quotes
:"8bit" #=> :"8bit"
# symbols with certain characters will be displayed as quoted
:"foo-bar" #=> :"foo-bar"
:"foo bar" #=> :"foo bar"
:"null_byte_\0" #=> :"null_byte_\x00"
Method names may be one of the operators or must start a letter or a character with the eight bit set. It may contain letters, numbers, an _
(underscore or low line) or a character with the eight bit set. The convention is to use underscores to separate words in a multiword method name:
def method_name
puts "use underscores to separate words"
end
Ruby programs must be written in a US-ASCII-compatible character set such as UTF-8, ISO-8859-1 etc. In such character sets if the eight bit is set it indicates an extended character. Ruby allows method names and other identifiers to contain such characters. Ruby programs cannot contain some characters like ASCII NUL (\x00
).
The following are examples of valid Ruby methods:
def hello
"hello"
end
def こんにちは
puts "means hello in Japanese"
end
Typically method names are US-ASCII compatible since the keys to type them exist on all keyboards.
Method names may end with a !
(bang or exclamation mark), a ?
(question mark), or =
(equals sign).