IRB 和 Rails 控制台都有一个很好的输出符号的方法,只在必要时引用转义它们。一些例子:
1.9.3p194 :001 > "@test".to_sym
=> :@test
1.9.3p194 :002 > "@Test".to_sym
=> :@Test
1.9.3p194 :003 > "@123".to_sym
=> :"@123"
1.9.3p194 :004 > "@@_test".to_sym
=> :@@_test
1.9.3p194 :005 > "test?".to_sym
=> :test?
1.9.3p194 :006 > "test!".to_sym
=> :test!
1.9.3p194 :007 > "_test!".to_sym
=> :_test!
1.9.3p194 :008 > "_test?".to_sym
=> :_test?
1.9.3p194 :009 > "A!".to_sym
=> :"A!"
1.9.3p194 :010 > "@a!".to_sym
=> :"@a!"
您将如何自己执行此操作,以便您可以执行以下操作:
puts "This is valid code: #{escape_symbol(some_symbol)}"