Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Syntax, using numbers in symbols?

I'm new to ruby and Chef and am running to an issue with syntax when defining attributes in my cookbook. Below is relevant code:

default[:my_cookbook][:stuff] = {
:foo_bar => {
:grok => ['Hi'],
:2grok => ['Bye'],
...

It appears I can't use a number to begin 2grok.. Is there a way to escape this, or what would be the proper syntax to use '2grok'?

like image 422
mxmxx Avatar asked Dec 10 '22 15:12

mxmxx


1 Answers

If you want to start a symbol with a digit, you need to enclose it in quotes:

:'2grok' => ['Hi']

If you use double quotes, ruby interpolates string inside:

:"#{1 + 1}grok"

Also, you can use percent-notation:

%s{2grok}

Finally, you can get the symbol by calling to_sym method on a String:

'2grok'.to_sym => ['Hi']
like image 58
Mladen Jablanović Avatar answered Dec 27 '22 10:12

Mladen Jablanović