本文介绍了如何理解“ActiveRecord::Migration[5.2]"导轨/红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • ActiveRecord 是一个类
  • ActiveRecord::Migration 是一个模块
  • [5.2] 是一个带有一个 Float
  • 的数组
  • ActiveRecord is a Class
  • ActiveRecord::Migration is a module
  • [5.2] is array with one Float

但是ActiveRecord::Migration[5.2]是什么意思?

推荐答案

Ruby 允许您像这样定义 [] 方法:

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

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

然后你可以这样做:

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

现在 Foo["a"] 打印 a.

Rails 通过这里的代码利用了这一点:https://github.com/rails/rails/blob/66cabeda2c46c582d19738e1318be8d59584cc5b/activerecord/lib/active_record/migration.rb#L543

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

因此,您示例中的 [5.2] 不是内部带有浮点数的数组,而是对 ActiveRecord::Migration.[] 方法的调用,其中 5.2 为论点.

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.

这篇关于如何理解“ActiveRecord::Migration[5.2]"导轨/红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:44