Problem in hash and symbol

Hi everybody
I have a question about hash and symbol. when I use symbol for key in
hash , it didn’t for me. what is the problem ?

(example : hash = { :odd => [ 1 , 3 , 5 ] , :even => [ 2 , 4 , 6 ] , :7
=> “chance” , “number” => 1 } )

On Fri, Sep 10, 2010 at 11:19 PM, Amir E. [email protected]
wrote:

Hi everybody
I have a question about hash and symbol. when I use symbol for key in
hash , it didn’t for me. what is the problem ?

(example : hash = { :odd => [ 1 , 3 , 5 ] , :even => [ 2 , 4 , 6 ] , :7
=> “chance” , “number” => 1 } )

A symbol cannot start with a number:

irb(main):005:0> :7
SyntaxError: compile error
(irb):5: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT
or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
from (irb):5
from :0
irb(main):006:0> :7y
SyntaxError: compile error
(irb):6: syntax error, unexpected tINTEGER, expecting tSTRING_CONTENT
or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
from (irb):6
from :0

Jesus.

On Fri, Sep 10, 2010 at 2:19 PM, Amir E. [email protected]
wrote:

Hi everybody
I have a question about hash and symbol. when I use symbol for key in
hash , it didn’t for me. what is the problem ?

(example : hash = { :odd => [ 1 , 3 , 5 ] , :even => [ 2 , 4 , 6 ] , :7
=> “chance” , “number” => 1 } )

Posted via http://www.ruby-forum.com/.

Looking at that, you’ve got two symbols (:odd, :even) used as keys, a
string (“number”) used as a key, and a great big honking syntax error
(:7) where a key should be. If you want to use the string “7”
converted to a symbol (whether its as a key or otherwise), the Ruby
syntax for that is :“7”, the only symbols that don’t need quotes are
ones that would be valid ruby identifiers – and 7, standing alone, is
an integer, not an identifier.

So the hash {:odd => [1, 3, 5], :even => [2, 4, 6], :“7” => “chance”,
“number” => 1} seems to (maybe) be what you’re looking for.

On Sep 10, 2010, at 14:19 , Amir E. wrote:

Hi everybody
I have a question about hash and symbol. when I use symbol for key in
hash , it didn’t for me. what is the problem ?

(example : hash = { :odd => [ 1 , 3 , 5 ] , :even => [ 2 , 4 , 6 ] , :7
=> “chance” , “number” => 1 } )

what isn’t working for you? the :7 ? that’s not valid syntax for ruby
symbols. You’ll have to use the :“extended symbol” notation: