unless in Ruby on rails

By: Brian Marick Viewed: 153236 times  Printer Friendly Format    


The word “unless” is in the English language for a reason. Contrast “unless the judge is unfair, you’ll win” with “if the judge is not unfair, you’ll win.” Negations - especially negations of already negative ideas can be confusing. The same is true of Ruby code:

unless unfair(judge)

winner = you

end

if not unfair(judge)

winner = you

end

You can also use unless with the one-line form:

array.pop unless array.empty?



Most Viewed Articles (in Ruby )

Latest Articles (in Ruby)

Comment on this tutorial