recent posts related to the ruby programming language

A Ruby Monte Carlo simulation of coin flips

For some reason or another today I was curious about this question: If you flipped a coin ten times, what are the odds that the coin would come up heads ten times, or tails ten times)?

I'm sure there is a way to determine this statistically, but I don't know how to do that, so, being new to Ruby, I wrote a little Ruby simulation program — essentially a Monte Carlo simulation of the problem — to find the answer. (Pretty boring, I know, but hey, I was bored, interested in learning Ruby, and didn't feel like reading any more of The Stand right now.)

Ruby command line arguments

Ruby FAQ: How do I read command line arguments in a Ruby script (Ruby command line args)?

To read command line args in a Ruby script, use the special Ruby array ARGV to get the information you need. Here are a few examples.

1) Getting the number of command line args

To get the number of command line arguments passed in to your Ruby script, check ARGV.length, like this:

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.

Ruby stdout and stderr

Very cool, thanks to a comment from a reader I just learned about the Ruby $stdout and $stderr variables. In short, you can write to STDOUT and STDERR in Ruby using code like this:

$stdout.puts "stdout"
$stderr.puts "stderr"

If you put those two lines in a Ruby script, and then run the script like this:

ruby test.rb > stdout 2> stderr

you'll find that the stdout and stderr files will contain the two different strings.

A Ruby JPEG example - Use Ruby to get the date and time from a JPEG file

A few weeks ago when I upgraded my iPhone 3G to iOS 4, I hooked up my iPhone to my old Windows computer, where it was originally sync'd. Downloading my iPhone photos onto my Windows XP computer was a pain, and worse than that, in the process, the iPhone photos were pulled off the phone in some random order, so all the photos are completely out of order when I look at them in the Windows Explorer or the Mac Finder.

Ruby developer jobs

During the past year I've worked with all sorts of programming languages, technologies, and frameworks, including Ruby and Rails; Java, Spring, Struts, and JSF; and PHP, CakePHP, and Drupal.

A Ruby script to remove binary (garbage) characters from a text file

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.

Ruby - How to convert ASCII decimal (byte) values to characters

Problem: You have a byte value, or a string of byte values, and you want to use a Ruby script to convert each byte to its equivalent ASCII character.


I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, the file had a bunch of binary "garbage" characters in it, and I wanted a clean version of the file that contained only printable ASCII characters in it.

Ruby - How to convert characters to ASCII decimal (byte) values

Problem: You have a character, or a string of characters, and you want to use a Ruby script to convert each character to its ASCII decimal (byte) value.


I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, all you have to do to convert a character in Ruby to its equivalent decimal ASCII code is use the ? operator in front of it, like this: