phd
phd

Reputation: 819

Simple ruby syntax if statement. Is this possible?

I'm really new to programming and would like to know if this is possible:

def lock(a,b,c,d)
if (a == 3 || a == 5 || a == 7 && b == 2 && c == 5 || c == 6 && d == 8 || d == 9 || d == 0)
  "unlocked"
else
  "locked"
end

If it isn't possible, why?

So when I pass in: lock(3,1,1,1) it should be locked.

Should I do this differently? Is there a better way?

Upvotes: 0

Views: 57

Answers (2)

G B
G B

Reputation: 3024

I would use include? to make it more readable:

if ([3,5,7].include?a and b == 2 and [5,6].include?c and [8,9,0].include?d)

Upvotes: 3

jon snow
jon snow

Reputation: 3072

You should have to add bracket properly as per your logic. Like this,

((a == 3 || a == 5 || a == 7) && (b == 2) && (c == 5 || c == 6) && (d == 8 || d == 9 || d == 0))

Upvotes: 0

Related Questions