Don't understand the 'UNLESS' and 'IF'


#1


Could someone please clarify why do we use unless in the following lines respectively?

  unless n.is_a? Integer
    return "n must be an integer."
  end

Here is what I understand from the above code : UNLESS n is an integer, it will return "n must be an integer", i.e. given that n is an integer, it will not return "n must be an integer". Or given that if n = a (an alphabet), it will return "n must be an integer" which obviously is not, since alphabet is not an integer.

  if n <= 0
    return "n must be greater than 0."
  end

Likewise, here is what I understand from the above code : if n is smaller or equal than 0, it will return "n must be greater than 0". So say if n = - 1 (which is obviously smaller than 0), but according to the code, it will return "n must be greater than 0" (obviously -1 is not).

Aforementioned code are very contradicting at least from my understanding. This has been really bothering me. I think I might have misunderstood something or my understanding in this topic is limited. I hope someone can enlighten me preferably in a simple way as I am new to coding.

`


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.