Niels Kristian
Niels Kristian

Reputation: 8845

Make ruby raise an error on duplicate key definition in hash

Is it possible to make ruby raise an error on a duplicate key definition in a hash. E.g.

{a: 1, b: 2, a: 3}

Currently this just gives a warning like so:

warning: key :a is duplicated and overwritten on line 1

I would rather get an exception

BTW: I'm on ruby 2.7

Upvotes: 0

Views: 311

Answers (1)

Pascal
Pascal

Reputation: 8646

This is a parse timew warning. I don't think you can generate an exception: https://github.com/ruby/ruby/blob/master/parse.y#L12201

Upvotes: 1

Related Questions