Posts in the “ruby” category

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:

JRuby Toolkit Robot example - how take a screenshot of your desktop

Here's some sample JRuby code that I just used to take a screenshot of my desktop. It uses the Toolkit and 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.

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.

First Mac Ruby Appscript examples

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.