I was confused when I saw such a piece of code in Ruby.
obj = [ a: "b", x: "y" ]
The parentheses are for the ** array **, but the contents are written as ** hashes **.
It's a code that actually works in business, so it's definitely not a syntax error.
You can see it immediately by executing it with irb etc. The equivalent code is below.
obj = [{ a: "b", x: "y" }]
#=> [{:a=>"b", :x=>"y"}]
In short, an "array whose elements are hashes".
In retrospect, I was able to ** omit the hash curly braces ** at the end of the method call arguments. Array literals may be treated in the same way as arguments.
def mtd(*args)
p args
end
mtd 1, 2, a: "b", x: "y"
#=> [1, 2, {:a=>"b", :x=>"y"}]
Recommended Posts