Quick and Easy Ruby Script
November 13, 2007 · by Scott Fradkin
One of my goals when checking out a programming language that is new to me is to find some kind of use for it other than what I’ve read in a book. While Ruby seems to have found a niche with the Rails web framework, I was also interested in other uses for it. Believe it or not, I’ve never really learned Perl, and I’m not the greatest with shell scripts, so it’s nice to have learned enough Ruby to perform some helpful tasks.
Ruby is an interesting language. You can use it as an Object Oriented language, or you can use it more like a functional programming language. What’s really nice is that because it’s currently an interpreted language and not compiled, you can use it as a general purpose scripting language.
I’ve created a really simple script that runs as a cron job to run the ‘emerge’ program, compile the results, and email them to me. Quite helpful. The concept can be used to gather results from running anything that has output. The script is below. Even though you specify from and to addresses in the Ruby send_message() call, I found that at least with my internet provider I still needed to specify Subject, From, To, and User-Agent mail headers otherwise the mail got bounced.
require 'net/smtp'result = `emerge -upDv world`
msg = "Subject: Packages to Emerge\r\n"msg += "From: FromName <from@name.com>\r\n"msg += "To: ToName<to@name.com>\r\n"msg += "User-Agent: Ruby Mailer\r\n"msg += resultmsg += "\r\n"Net::SMTP.start('mail.name.com') do |smtp| smtp.send_message(msg, 'from@name.com', ['to@name.com'])end
Filed in: Team Member Blog Comments (0)

Comments
There are no comments for this entry.
Commenting is not available in this section entry.