Most developers are aware of the Switch statement that compares a variable to specific cases and performs logic based on if it matches the case criteria. In Ruby they decided to implement this as simply a Case statement with when clauses specifying logic to be performed if the when clause is true.

case year
when 2000
    puts "The world was supposed to end this year!"
when 2012
    puts "Now the world is supposed to end this year!"
else
    puts "I wonder what year the world will supposed to end after 2012?"
end

As you can see from the above example it functions the same as in other languages with a slightly different syntax where instead of default you use end and instead of cases you use when

Case statements with multi value When Clauses

In Java, PHP and other programming languages the case statements can fall through so that you can define some logic to occour for multiple values. Ruby is no different in that it too allows you to have multiple values in each when statement the difference is you simply sperate the values by a ,(comma) when defining your when statements.

case year
when 2000, 2012
    puts "The world is going to end!"
else
    puts "Is the world going to end?"
end

Case statement with no value defined

If you simply prefer to write case statements instead of if statements then you can define a case statement that has no value and instead can specify the comparison logic in each when clause.

aString = "testing"
 
case 
when aString="testing"
    puts "What are you testing."
else 
    puts "This is for real everyone!"
end

The Case statement in Ruby breaks no new ground however it does provide you with an alternative method of writing your basic if statements. Good to know but as for which you should utilize it is really up to your own preference.

, , ,

Remembering the correct command prompts across different platforms can be confusing. To make things easier on myself I have compiled a short list of the commands and their options that are commonly used to create and delete files and folder in DOS. Including how to delete a folder and all its contents.

How to Delete a file

Deleting files from the DOS prompt is done by the del command. To use it you simply specify the single file you want deleted or the wild card pattern to match files against.

Deleting a Single file

del example.txt

Deleting all .jpg files

del *.jpg

In addition to deleting files in a single directory you can delete files from subfolders as well by using the /s switch. This option says to delete files matching the pattern in a ll subfolders. I highly suggest using this switch only if you know what you are doing.

Deleting all files within a folder and its subfolders

del  /s testFolder/*

The most important thing to remember is that the del command will delete files but will leave the folder structure intact.

How to delete Folders

There are two commands available to delete folders rd and rmdir. Both commands perform the same functionality so you can use which ever comes more natural to you. To delete a folder you will first have to delete its contents.

Deleting a Single Folder

rd myFolder
rmdir myFolder

To delete a folder as well as its contents including files and sub folders you will use the /s switch. If you don’t want to have the command line prompt you with are you sure you want to delete each file you can also use the /q switch. This causes the command to run in quiet mode meaning it will assume y for all the prompts.

Deleting a Folder and all its contents

rd /s /q myFolder
rmdir /s /q myFolder

Creating a new Directory or Folder

To create a folder you use the md or mkdir commands. When running the command simply type the name of the folder you want to create. If you want to create a sub folder as well simply pass in the full path and it will create the parent folders if they don’t exist.

Creating a single Folder

md myFolder
mkdir myFolder

Creating a sub Folder and its parents

md myFolder/Folder2/Folder3
mkdir myFolder/Folder2/Folder3

To be sure creating and deleting files and folders is a command that by far is done mostly through the Operation Systems User Interface. However there are times that working from the dos prompt (command line) is a necessity, in those case simply remember del, rd, and md commands.

, , , , , , ,