Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square brackets in ActiveRecord::Migration class name? [duplicate]

In Ruby on Rails 5.2, migration files contain a class that looks like this.

class MyMigration < ActiveRecord::Migration[5.2]

I know that the [5.2] syntax is specifying what version of rails the migration targets, but I don't understand how this works. Migration[5.2] is not a valid Ruby class name.

What is this square bracket syntax, and how is it not causing ruby syntax errors?

like image 210
fourseventy Avatar asked Oct 24 '25 18:10

fourseventy


1 Answers

Ruby objects can have [] methods. They are used in the Array class, but any class can implement them.

def []()
  ...
end

Since classes are full objects, they too can have square bracket methods.

like image 152
Marlin Pierce Avatar answered Oct 26 '25 09:10

Marlin Pierce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!