0
  • ActiveRecord is a Class
  • ActiveRecord::Migration is a module
  • [5.2] is array with one Float

But what means ActiveRecord::Migration[5.2]?

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Matrix
  • 3,458
  • 6
  • 40
  • 76

1 Answers1

5

Ruby allows you to define a [] method like this:

  class Foo
    def [](bar)
      puts bar
    end
  end

Then you can do:

x = Foo.new
foo["baz"] # prints baz

This also works with a class method [], not just with an instance one:

class Foo
  def self.[](bar)
    puts bar
  end
end

Now Foo["a"] prints a.

Rails is taking advantage of this through this code here: https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activerecord/lib/active_record/migration.rb#L543

So the [5.2] in your example is not an array with a float inside, it's a call to the ActiveRecord::Migration.[] method with 5.2 as the argument.

spike
  • 9,794
  • 9
  • 54
  • 85