Posts in the “ruby” category

Ruby mail program - how to find all email messages I have read but not replied to

Here is the source code for a Ruby mail program I wrote that looks through my INBOX and finds all the email messages I have a) read but b) have not replied to.

This Ruby script comes in very handy on a project I'm working on now where we exchange 50-75 email messages per day. After a little while there are so many messages I can't remember if I replied to the important messages, so this program helps whittle down the list and just shows the ones I have read, but have not replied to. (The ones I haven't read are a lot easier to spot.)

Ruby natural logarithm example

Still reading Calculus Made Easy, they note that 'e' (the natural logarithm, or natural log) is the limit of the following series:

1 + 1/1! + 1/2! + 1/3! ...

To test this I created the following Ruby natural log program.

How to print a Ruby hash sorted by value

I recently needed to print the information in a Ruby hash, with the results sorted by value. Here's a general recipe on how to print the hash results sorted by value. I've created a sample hash, and populated the hash with sample data, to show how this works.

First, here's the sample code, using the first name of each person as the key, and the last name as the value of the key/value pair:

A JRuby program that runs the Java Robot class

This is just my second JRuby program, but I thought I might as well go for the gusto. This Ruby/JRuby program creates an instance of a Java Robot class (java.awt.Robot), then moves the mouse to a position where it clicks the Minimize button on a full-screen window (assuming a display resolution of 1024x768). Warning: if you have something else in that location it will click that instead!

Here's the Ruby/JRuby code:

How to run JRuby from a Ruby script on a Windows PC

I'm doing some crazy things at the moment, basically calling JRuby from a Ruby script on a Windows 2000 system. I'm doing this because there are a bunch of JRuby scripts that I want to run sequentially, and I also want to check for errors after each run, so what better way to invoke them and look for resulting errors than with Ruby, especially on a Windows system? :)

Ruby system example - how to make system calls and redirect output to a file

I still have a lot to learn about Ruby, but here's a Ruby script that runs a series of system commands (Kernel.system()), which in my case means calling a series of JRuby scripts. I send all of the output from this script and from the system calls to a file by (a) writing directly to the file using Ruby and (b) redirecting STDOUT when making each system() call. I think this is a hack, but I can't find a better way to redirect STDOUT.

Ruby read file example - read a file as a string

Here's a Ruby "read file" example program that shows how to read a file as one string. You just have to pass a file name in to the get_file_as_string method, and this Ruby method will read in all the records from the file, and return the contents of the file as a string.

Ruby mail - find all unique email addresses in your inbox

Here's a sample Ruby mail program that I created that finds and prints all of the unique email addresses in my IMAP inbox. Hopefully the source code is readable enough that it doesn't need much description. The only hard part is trying to figure out how to get the email address from the Envelope, and that's only because the documentation is hard to find.

Ruby mail program

Here's my Ruby IMAP mail program source code:

A Ruby script to find hidden messages in text

Here's a simple Ruby program that opens a text file, then uses a series of simple algorithms to look for hidden words in the text. For instance, it looks at only odd words, only even words, then looks at Nth characters, Nth words, and also Fibonacci words and characters.

Ruby mail example - search an IMAP mail server

Here is a Ruby mail program that I use to search my IMAP mailbox with combinations of search criteria. It is a mixture of several different Ruby-based approaches I found on the Internet for searching email systems.

Note that you don't need all the comments at the front as I've shown. It's just that I can't remember all the crazy flags that can be used, and at this time I'm not going to take the time to write a better mail system interface, although IMHO a better interface is surely needed.