recent posts related to the ruby programming language
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:
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.
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 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.
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.
I just finished a Ruby client program that gets the current weather for multiple cities, using the Yahoo! Weather RSS Feed, and then mails the output to me. (Besides demonstrating this Ruby weather capability, it also shows how to send a simple mail message using the Ruby mail API.)
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.
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.
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:
In a previous tutorial I wrote about how to sort an array of Ruby objects by one field in the object. In today's tutorial I'd like to demonstrate how to sort an array of Ruby objects by multiple attributes (or fields) of the class contained by the array.