Can I overload / create a postfix unary operator?

I would like to create two unary postfix operator !and !!a class Numericin ruby. I determined it so far

class Numeric
  def !
    (1..self).inject(:*)
  end

  def !!
    if self.even?
      (2..self).step(2).inject(:*)
    else
      (1..self).step(2).inject(:*)
    end
  end
end

However, this means that I should call 5.!and 5.!!. I know that you can define unary prefix operators such as +@and -@, but I was wondering if I can define a unary postfix operator so that I can name 5!and 5!!instead of 5.!and 5.!!.

+4
source share
1 answer

. -, Ruby , . -, , , , foo!, foo!, foo ! .

+4

Source: https://habr.com/ru/post/1661528/


All Articles