Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Forked from misfo/symbol_literals.rb
Last active August 29, 2015 14:22
Show Gist options
  • Save gene1wood/a70fa860cfa2a527879b to your computer and use it in GitHub Desktop.
Save gene1wood/a70fa860cfa2a527879b to your computer and use it in GitHub Desktop.
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
$ ruby symbol_literals.rb
valid as first char:
@$_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
valid as middle char:
_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
valid as end char:
!_=?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
valid as a single character
(eval):1: warning: invalid character syntax; use ?\s
`~!%^&*-_+|<>/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
$ # Note the eval warning relates to the disallowed symbol of :?
def symbol_chars_are_valid(symbol_chars)
# :aaa == :"aaa" if it has valid Symbol literal chars
# puts "trying #{symbol_chars}"
x = eval ":#{symbol_chars} == :#{symbol_chars.inspect}"
rescue Exception
false
end
chars_to_try =
'`~!@$%^&*()-_+={}[]|\;:"<>,.?/'.split('') + ["'"] +
('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
puts "valid as first char:"
puts chars_to_try.select {|c| symbol_chars_are_valid "#{c}aa" }.join ''
puts "valid as middle char:"
puts chars_to_try.select {|c| symbol_chars_are_valid "a#{c}a" }.join ''
puts "valid as end char:"
puts chars_to_try.select {|c| symbol_chars_are_valid "aa#{c}" }.join ''
puts "valid as a single character symbol"
puts chars_to_try.select {|c| symbol_chars_are_valid "#{c}" }.join ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment