Alexander Forbes-Reed
Alexander Forbes-Reed

Reputation: 2975

Ruby negated-if statements

I have a statement checking if a string contains another; if it doesn't, it runs some code. I don't think I'm doing it in a very optimised way. Here is a code snippet:

if blocks_in_progress.include? ('|' + blocks[i])
else
    block = blocks[i]
    break
end

Upvotes: 0

Views: 3338

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118289

A one liner:

 break block = blocks[i] unless blocks_in_progress.include? ('|' + blocks[i])

Upvotes: 3

James Billingham
James Billingham

Reputation: 770

Bit of a Ruby newbie here, but as far as I understand it, the following is the preferred way:

unless blocks_in_progress.include? ('|' + blocks[i])
   block = blocks[i]
   break
end

Source: https://github.com/bbatsov/ruby-style-guide (syntax section)

Upvotes: 4

Related Questions