Why does white-space affect ruby function calls?

前端 未结 2 1438
抹茶落季
抹茶落季 2020-12-02 00:50

I get a syntax error with this code

render json: {
    \"what\" => \"created\", 
    \"whatCreated\" => \"thing\",
    \"htmlOutput\" => render_to_s         


        
2条回答
  •  死守一世寂寞
    2020-12-02 01:14

    the thing is, that method in ruby can be run with or without parentheses. for example, you can run Array.new 1,2 and ruby knows that it receives the arguments after the space. and you can also run Array.new(1,2) and ruby knows the args are inside the parentheses.

    but, when you run Array.new (1,2) , ruby thinks it will receive arguments after the space but actually it receives a tuple (1,2), and basicaly its exactly the same as Array.new((1,2))

    so bottom line:

    Array.new (1,2) == Array.new((1,2)) and thats a syntax error because (1, 2) literal is not a valid one

提交回复
热议问题