什么是Ruby类的<=运算符?

Nic*_*ilt 8 ruby operators

以下代码段来自rails代码

  def rescue_from(*klasses, &block)
    options = klasses.extract_options!

    unless options.has_key?(:with)
      if block_given?
        options[:with] = block
      else
        raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
      end
    end

    klasses.each do |klass|
      key = if klass.is_a?(Class) && klass <= Exception
        klass.name
      elsif klass.is_a?(String)
        klass
      else
        raise ArgumentError, "#{klass} is neither an Exception nor a String"
      end

      # put the new handler at the end because the list is read in reverse
      self.rescue_handlers += [[key, options[:with]]]
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

注意运算符<=

那是什么?

Gre*_*ell 13

有关Module(以及Classes)公开的所有比较运算符的文档,请参见http://ruby-doc.org/core/classes/Module.html#M001669.

在这种特殊情况下:"如果mod是其他的子类或者与其他类似,则返回true.如果两者之间没有关系,则返回nil.(根据类定义考虑关系:"A <B <暗示"A <B")."


mae*_*ics 6

is_a?如果接收者类是参数的子类,它与返回true 的方法相当; 考虑:

Fixnum.superclass # => Integer
Fixnum <= Integer # => true
Run Code Online (Sandbox Code Playgroud)