Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the <= operator on Ruby Classes?

Tags:

operators

ruby

following snippet is from rails code

  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

Notice the operator <=

what is that?

like image 549
Nick Vanderbilt Avatar asked Jul 23 '10 18:07

Nick Vanderbilt


People also ask

How do you check greater than in Ruby?

Greater Than(>) operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.

What does === mean in Ruby?

In Ruby, the === operator is used to test equality within a when clause of a case statement. In other languages, the above is true. To my knowledge, Ruby doesn't have true operators, they are all methods which are invoked on the LHS of the expression, passing in the RHS of the expression.

What is the name of this operator === Ruby?

Triple Equals Operator (More Than Equality) Our last operator today is going to be about the triple equals operator ( === ).

How do you use operators in Ruby?

To define unary + and unary – operators, use method names +@ and -@ to avoid ambiguity with the binary operators that use the same symbols. The != and !~ operators are defined as the negation of the == and =~ operators. In Ruby 1.9, you can redefine !=


2 Answers

See http://ruby-doc.org/core/classes/Module.html#M001669 for documentation on all the comparison operators exposed by Modules (and therefore Classes).

In this specific case: "Returns true if mod is a subclass of other or is the same as other. Returns nil if there‘s no relationship between the two. (Think of the relationship in terms of the class definition: "class A < B" implies "A < B")."

like image 118
Greg Campbell Avatar answered Sep 24 '22 03:09

Greg Campbell


It's comparable to the is_a? method which returns true if the receiver class is a subclass of the argument; consider:

Fixnum.superclass # => Integer
Fixnum <= Integer # => true
like image 43
maerics Avatar answered Sep 26 '22 03:09

maerics