ruby-on-railsrubydsl

Is this Ruby syntax or is it a DSL and how can I learn Ruby syntax or Rails DSL?


For example here is some code in the routes.rb file

match 'products/:id' => 'products#show', via: :get

I would think match is a method without parens, so that is something I am used to. Then the match method takes two arguments. But is

'products/:id' => 'products#show'

supposed to be an implied hash? I can't create a hash using no braces like this seems to be doing here

my_hash = 'p' => 'x'

Then the last piece doesn't work in my irb substituting the values but keeping the structure.

I know how to read the docs and just copy paste and sub out values for what I need and make things work, but I want to understand the syntax so that I can just write the code without having to feel like it is all arbitrary and needing the docs to show me exactly what to type.


Solution

  • Regarding the hash, it is, indeed, a hash. When creating a new hash, Ruby expects it to be defined within curly braces ({}). However, it allows it to be declared without braces when passing it as an argument to a method (when it is the last argument). Same thing happens with via: :get.

    You can read more about it here.