Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference without curly braces when using hash as an argument?

It seems there is no difference between these two expressions.

h = {a: 1, b: 2}
h.merge({c: 3, d: 4})
h.merge(c: 3, d: 4)

Is there a problematic case if I omit the curly braces when using Hash as an argument?

like image 860
ironsand Avatar asked Jan 28 '26 05:01

ironsand


1 Answers

No, there is no difference between both versions.

Furthermore, it is a common Ruby/Rails idiom to omit the curly brackets when the hash is the last argument to a method. Compare the following common examples:

validates :foo, { presence: true }
validates :foo, presence: true

link_to "Foos", foo_path, { class: "foo" }
link_to "Foos", foo_path, class: "foo"
like image 152
spickermann Avatar answered Jan 30 '26 22:01

spickermann