arunt arunt - 3 months ago 7
Ruby Question

Getting a syntax error in production but not in development (identical files)

I checked with Meld - the files are completely identical. The following code works fine in my local machine but my production server went down because of it:

- selections = {"All": 0, "Last 30 days": 30, "Last 15 days": 15, "Last 7 days": 7,"Last 24 hours": 1}


Changing
"All" : 0
to
"All" => 0
etc fixed it.

My guess is that this is because my
Ruby version
is 2.3.0 while on the production server it is 2.1.5. Or is this something else? It was my first time crashing the website, and this seems like a silly thing over which I tripped.

Answer

You are defining Ruby hashes there. On Ruby 2.2 and above, all three of these variants are equivalent:

{:a => "hello", :b => "World"} # allowed since always
{a: "hello", b: "World"}       # allowed since Ruby 1.9
{"a": "hello", "b": "World"}   # Allowed since Ruby 2.2

The syntax used in the last example however was only added in Ruby 2.2. Since you are running an older version of Ruby on the server, you have to adapt the syntax used in your code to one of the other two variants. Alternatively, you can update the Ruby version on the server to something newer.

As a word of advice finally: You should generally closely match the version of Ruby used on production and during development. The language evolves and adds features. Things like that should be caught early. At least run your tests on the same Ruby version your server uses.

Comments