If you ever see the following error message when running a Ruby mail script that uses the IMAP library it may not be as bad as it seems:
Posts in the “ruby” category
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.
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:
After a fairly large number of emails I've started working on my type-ahead, predictive text editor project. In support of this effort I'm looking at different algorithms to best predict the word the user next wants to type. The first part of this is looking at documents I've written in the past, and analyzing the frequency of word occurrences within those documents.
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:
Earlier tonight I installed JRuby on a Windows XP system, and it was about as easy as anything I've installed. Here were the steps on my computer:
Here's some sample JRuby code that I just used to take a screenshot of my desktop. It uses the
Robot classes from Java to make it all happen. It could probably be a little shorter, but I don't know much about JRuby yet. I also had a problem getting the Java
File class to work properly, and referencing it as shown was the only way I could get it to work.
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? :)
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.
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.
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:
I get the following error messages some times when running JRuby scripts:
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.
I just took a brief look at Ruby Appscript as a potential replacement for AppleScript on Mac OS X. So far it looks promising, and works on Mac OS X 10.6 (Snow Leopard) just fine.
The hardest part about working with it yet has been finding a few examples to get going with. Based on my forty-five minutes of working with it just now, here are a couple of quick Ruby Appscript examples that might help get you going a little faster.