In Rails, you might see:
validates :name, presence: true
Is presence: true a keyword argument or a hash?
Ruby allows both keyword arguments and regular arguments, including hashes. Before Ruby 2.7, keyword arguments were just hashes passed as the last argument. From Ruby 2.7 and especially Ruby 3.0 onward, keyword arguments are now strictly separated from positional hashes.
So depending on the Ruby version, this line:
Could be interpreted as:
In Rails, most DSL methods like validates are designed to work with either form.
Rails defines validates like this:
Here, the presence: true part is extracted as a hash from the arguments array.
In most Rails DSLs, options like presence: true are passed as trailing hashes. No need to worry unless you’re designing custom APIs or methods.
Work with our skilled Ruby on Rails developers to accelerate your project and boost its performance.
Hire Ruby on Rails Developer