【问题标题】:how to understand "ActiveRecord::Migration[5.2]" rails / ruby [duplicate]如何理解“ActiveRecord::Migration[5.2]”rails / ruby​​ [重复]
【发布时间】:2019-08-18 01:58:20
【问题描述】:
  • ActiveRecord 是一个类
  • ActiveRecord::Migration 是一个模块
  • [5.2] 是一个数组,有一个 Float

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5


    【解决方案1】:

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

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

    那么你可以这样做:

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

    这也适用于类方法[],而不仅仅是实例方法:

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

    现在Foo["a"] 打印一个。

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

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

    【讨论】:

    • 我知道 ruby​​ 允许定义 [] 但我不考虑 self.[] :) 但这是逻辑! ;) 谢谢
    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多