Things to be considered while using UNLESS / IF in Ruby

If statement is simple and every body has ground knowledge of what and how to use. I guess everybody has knowledge of using Boolean Expression in IF statement.

However the tricky part is, we need to think differently while writing Boolean Expression in UNLESS statement.

Example;

if x == 1 && y == 2
  // Do Something when conditions meet
end

If you want to do same job when conditions do not meet at all using UNLESS

unless x == 1 && y == 2
  // Do Something when conditions meet
end

This is WRONG!!

Right example:

unless x == 1 || y == 2
  // Do Something when conditions meet
end

Think the Boolean Algebra

In electrical and computer engineering, De Morgan’s laws are commonly written as:

\overline{A \cdot B} \equiv \overline {A} + \overline {B}

and

\overline{A + B} \equiv \overline {A} \cdot \overline {B},

where:

  •  \cdot is a logical AND
  • + is a logical OR
  • the overbar is the logical NOT of what is underneath the overbar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s