Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




Ruby Programming
Previous Page Home Next Page

If and Unless Modifiers

Ruby shares a neat feature with Perl. Statement modifiers let you tack conditional statements onto the end of a normal statement.

mon, day, year = $1, $2, $3 if /(\d\d)-(\d\d)-(\d\d)/
puts "a = #{a}" if fDebug
print total unless total == 0

For an if modifier, the preceding expression will be evaluated only if the condition is true. unless works the other way around.

while gets
  next if /^#/            # Skip comments
  parseLine unless /^$/   # Don't parse empty lines

Because if itself is an expression, you can get really obscure with statements such as:

if artist == "John Coltrane"
  artist = "'Trane"
end unless nicknames == "no"

This path leads to the gates of madness.
Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire