Amit Singh Tomar
Amit Singh Tomar

Reputation: 8610

if statement in ruby

How does this statement work?

if not a==b
  puts "amit"
else
  puts "ramit"
end

Could anybody tell me the use of not operator here?

Upvotes: 1

Views: 3959

Answers (3)

jigfox
jigfox

Reputation: 18177

if not a==b is equal to if !(a==b), if a!=b, unless a==b or unless not a!=b

If you don't know this I would recommend reading "The Well-Grounded Rubyist" from David A. Black

Upvotes: 3

Tom Gullen
Tom Gullen

Reputation: 61775

a == b returns true if they are equal.

The not operator inverts the answer, so:

not a == b returns true if they are NOT equal.

Upvotes: 3

Preet Sangha
Preet Sangha

Reputation: 65556

See here Ruby Logical Operators for a discussion.

not a==b is the same as !(a==b) they are both acceptable.

Upvotes: 2

Related Questions