Ruby:在一个类的子类之后 [number] 的语法名称 [英] Ruby: grammar name for [number] after subclass of one class

查看:40
本文介绍了Ruby:在一个类的子类之后 [number] 的语法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过示例学习 Ruby on Rails.我在迁移文件夹中看到这里是一个示例代码:

I'm learning Ruby on Rails by example. I see in migration folder here is one sample code:

class RemoveOrderListNumberAndStateFromOrderLists < ActiveRecord::Migration[5.0]
  def up
    remove_column :order_lists, :order_list_number
    remove_column :order_lists, :state
  end

  def down
    add_column :order_lists, :order_list_number, :string
    add_column :order_lists, :state, :integer
  end
end

我不知道的是在这一行:

The thing I don't know is at this line:

class RemoveOrderListNumberAndStateFromOrderLists < ActiveRecord::Migration[5.0]

我知道这个命令的意思是:创建一个类,它是 ActiveRecord::Migration 的子类,而且 5.0 是项目的 rails 版本.我不知道的是:在上述情况下,哪个 ruby​​ 语法名称允许您在 class 之后声明 [number] .我认为唯一的方法是数组,但在这种情况下不太可能.

I know this command means: create a class, that is a subclass of ActiveRecord::Migration and also 5.0 is rails version of project. Thing that I don't know is: which ruby grammar name that allow you declare [number] after class in above case. I think the only way is an array, but not likely true in this case.

谢谢

推荐答案

Ruby 语言带有很多所谓的语法糖",它允许您以不同的方式表达相同的想法.糖的目的是让程序员找到一种方式,以对个人最有意义的方式表达想法.

The Ruby language comes with a lot of what is called "syntactic sugar," which allows you to express the same ideas in different ways. The purpose of sugar is to allow the programmer to find a way to express the idea in a way that makes the most sense to that individual.

在您的情况下,方括号只是调用带参数的方法的另一种方式——没有功能上的区别.proc[]proc.call() 的另一种表达方式,所以括号只是隐藏了 call.通常,您只会看到这些括号与 lambda 结合使用.这种符号没有特定的语法名称"——它只是符号.

In your case, the square brackets are just another way to call a method with arguments -- there's no functional difference. proc[] is another way to say proc.call(), so the brackets are just hiding the call. Usually, you only see these brackets in conjunction with lambdas. There's no specific "grammar name" that goes with this type of notation -- it's just notation.

具体来说,这是一个在 ActiveRecord::Migration 上调用的类方法,它告诉类正在使用什么迁移版本.注意:如果您拥有 Rails 的发布版本,则不需要该符号——它应该在测试版完成后被删除.

Specifically, this is a class method being called on ActiveRecord::Migration that tells the class what migration version is being used. Note: if you have the release version of Rails, you shouldn't need that notation -- it should have been removed after the beta finished.

您可以在 GitHub 存储库

You can see where and how ActiveRecord::Migration uses that class method in the GitHub repository

    def self.[](version)
      version = version.to_s
      name = "V#{version.tr('.', '_')}"
      unless Compatibility.const_defined?(name)
        versions = Compatibility.constants.grep(/\AV[0-9_]+\z/).map { |s| s.to_s.delete('V').tr('_', '.').inspect }
        raise "Unknown migration version #{version.inspect}; expected one of #{versions.sort.join(', ')}"
      end
      Compatibility.const_get(name)
    end

这篇关于Ruby:在一个类的子类之后 [number] 的语法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆