More Conditional Logic in Ruby!

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Following from the previous tutorial on Conditional Logic in Ruby, look at this example:

# Let the user guess.
print "Enter heads or tails? "
hort = gets.chomp
unless hort == 'heads' || hort == 'tails' 
    print "I _said_ heads or tails.  Can't you read?\n"
    exit(1)
end

# Now toss the coin.
toss = if rand(2) == 1 then
    "heads"
else
    "tails"
end

# Report.
print "Toss was ", toss, ".\n"
print "You Win!\n" if hort == toss

Some more amazing if lore:

  1. The unless keyword is like if, but uses the opposite sense of the test: The code is run if the test is false.
  2. As with many things in Rubyif may look like a statement, but it is actually an expression, with a return value.
  3. The if has a postfix form where the condition comes last. This works with unless as well.

Conditional Logic in Ruby

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

This is the conventional use of the if in perl. Notice that there is no need for curly braces ({ and }). The body of the if ends with the appropriate keyword, endelse or elsif. The then word is generally optional, though you need it if you want to put start the body on the same line as the if, the way the last statement does.

Continue reading

Create a virtual encrypted disk within a file and mount it as a real disk

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

VeraCrypt is a free open source disk encryption software for Windows, Mac OSX and Linux based on (the now defunct) TrueCrypt 7.1a. Features include:

  • Creates a virtual encrypted disk within a file and mounts it as a real disk.
  • Encrypts an entire partition or storage device such as USB flash drive or hard drive.
  • Encrypts a partition or drive where Windows is installed (pre-boot authentication).
  • Encryption is automatic, real-time(on-the-fly) and transparent.
  • Parallelization and pipelining allow data to be read and written as fast as if the drive was not encrypted.
  • Encryption can be hardware-accelerated on modern processors.
  • Provides plausible deniability, in case an adversary forces you to reveal the password: Hidden volume (steganography) and hidden operating system.

Continue reading

Subversion Server Howto

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Your personal code repository in DkIT can be accessed using the following URL, replace [yourusername] with your login username.

https://svn.comp.dkit.ie/repos/[yourusername]

e.g. for bloggsj

https://svn.comp.dkit.ie/repos/bloggsj

Continue reading

How to Style Contact Form 7 Forms in WordPress

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

Contact Form 7 can manage multiple contact forms in WordPress, plus you can customize the form and the mail contents flexibly with simple markup. The form supports Ajax-powered submitting, CAPTCHA, Akismet spam filtering and so on.

The biggest downside is that the out of the box forms you add are very plain looking. Thankfully, Contact Form 7 can be easily styled using CSS in your WordPress theme. In this turorial, we will show you how to style contact form 7 forms in WordPress.

Continue reading

Parallel Assignment in Ruby

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

A parallel assignment sets several variables at once. All right sides are computed before any variable is assigned, so all right side values are computed with the old variable values. This is very convenient for swapping two values without using a temporary.

Continue reading

GIMP as a Photoshop Alternative

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

For those of you who are unaware of GIMP, here is how the software describes itself:

GIMP is the GNU Image Manipulation Program. It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

Continue reading

File I/O in Ruby

Watch out! This tutorial is over 7 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.

This version opens a file for writing, and writes its output to a file selected by the user. The second argument to open is the same as the second argument to a plain C fopen, except it will default to reading.

The object returned from open is class File.

Continue reading