Collection Initializers
Scripting languages like Ruby and Groovy recognize the importance of collections like lists and maps, treating them almost like first class objects. By doing so, they make it really easy to create collections like these and populate them all in one step.
For example, in Ruby here is how this is done:
# create a list
lst = [1, 2, 3, 4, 5]
# create a map
map = { "one" => 1, "two" => 2, "three" => 3 }
Not surprisingly, we see very similar syntax in Groovy...
// create a list
def lst = [1, 2, 3, 4, 5]
// create a map
def map = ["one":1, "two":2, "three":3]
And in case you missed it, one of the changes in C# 3.0 was the ability to initialize collections similar to the way it's done in these scripting languages (though the syntax is not *quite* as clean):
// create a list
var lst = new List<int> { 1, 2, 3, 4, 5 };
// create a map
var map = new Dictionary<string, int> { {"one", 1}, {"two", 2}, {"three", 3} };
IMO, the new collection initializers (along with property initializers) are one of the nicest changes in the new version of the .Net framework. And since I'm feeling nice, I won't show how this is accomplished in native Java. 
Saturday, June 14, 2008 8:23:10 PM (Eastern Standard Time, UTC-05:00)
C# | Groovy | Ruby
Getting the SysInternals Suite, Ruby style
John Robbins at Wintellect put out a nice blog post about using a PowerShell script to download and install the excellent suite of tools provided by Sysinternals. I absolutely love the *concept* of PowerShell and the ability to harness the power of .Net at the command prompt. The one downside to it is that I find the syntax to be a bit too verbose than what I normally expect out of a scripting language. YMMV.
Anyway, in an effort to stay on top of my (limited) Ruby skills, I have ported his implementation to Ruby:
#!/usr/bin/env ruby
require 'net/http'
require 'zip/zip'
def usage
puts ""
puts "Downloads and extracts all the tools from Sysinternals"
puts ""
puts "Usage: ruby download_sysinternals.rb <extract-directory> [<save-directory>]"
puts "Required Parameter :"
puts " <extract-directory> : The directory where the SysinternalsSuite.zip"
puts " tools are extracted."
puts "Optional Parameters :"
puts " <save-directory> : Saves SysinternalsSuite.zip to the specified"
puts " directory. If not specified, the .ZIP file "
puts " is not saved."
puts " -? : Display this usage information"
puts ""
puts ""
Kernel#exit
end
def create_directory_if_needed(dir)
File.makedirs(dir) unless File.exist?(dir)
end
def download_sysinternals_zip_file(download_dir)
puts "Downloading SysinternalsSuite.zip file"
url = URI.parse('http://download.sysinternals.com/Files/SysinternalsSuite.zip')
req = Net::HTTP::Get.new(url.path)
resp = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
puts "Saving SysinternalsSuite.zip to: #{download_dir}"
file_name = "#{download_dir}/SysinternalsSuite.zip"
delete_file_if_exists(file_name)
open(file_name, "wb") { |file| file.write(resp.body) }
puts "Finished downloading"
end
def extract_zip_file(zip_file, extract_dir)
puts "Extracting zip file to: #{extract_dir}"
Zip::ZipFile.open(zip_file) do |zip|
zip.each do |entry|
file_name = "#{extract_dir}/#{entry.to_s}"
puts "Extracting #{file_name}"
# you will need to run this as an admin if
# you have sysinternals stuff in "Program Files"
delete_file_if_exists(file_name)
entry.extract(file_name)
end
end
puts "Finished extracting zip file"
end
def delete_file_if_exists(file_name)
File.delete(file_name) if File.exist?(file_name)
end
# here we go!!!
usage if ( ARGV.empty? || ARGV[0] == "-?" )
extract_dir = ARGV[0]
save_dir = ARGV.length > 1 ? ARGV[1] : ENV["TEMP"]
# download the file
create_directory_if_needed(save_dir)
download_sysinternals_zip_file(save_dir)
# extract the zip file
create_directory_if_needed(extract_dir)
zip_file = "#{save_dir}/SysinternalsSuite.zip"
extract_zip_file(zip_file, extract_dir)
# clean up after ourselves
delete_file_if_exists(zip_file) if ARGV.length < 1
Tuesday, April 22, 2008 6:59:08 AM (Eastern Standard Time, UTC-05:00)
Powershell | Ruby | Sysinternals