Posts in the “ruby” category

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:

Using ActiveRecord without Rails

Here's a quick example of using Ruby ActiveRecord outside of Rails. In this case I'm saving a new record to an employees table in a MySQL database.

A Ruby web service client

In previous blog posts I showed how I created a Java web service and client using Apache Axis2. In those examples I showed how to read from web service methods that return a single object, and also an array or list of objects. In this post I'll show several sample Ruby programs that also read from those same Java web services.

Ruby FTP - A free Ruby script to throttle the FTP file upload speed

An interesting thing about developing software to work with an FTP server is that for some tests you need files to be uploaded to the FTP server very slowly. Usually you want software to run as fast as possible, but in my case I needed to be able to throttle the FTP upload speed to test portions of my code. (Specifically, I'm writing code to listen to Proftpd FTP server events, and I needed this to make sure all the STOR, DELE, RNFR, and RNTO events work as advertised (making sure the event notifications aren't sent until the event is complete.)

Ruby directory list - How to use Ruby to list files in a directory

It's so easy with Ruby to get a list of files in the current directory that I hesitate to write this, but hey, this blog is for me and my bad memory, so here's a quick note on how to use Ruby to get a list of files of a certain type in a directory.

To have a little fun with this, I'll use irb (the interactive Ruby shell environment) to show how to do this.

How to open a file and read its contents using Ruby

Ruby file FAQ: How do I open and read from a file in Ruby?

How many ways are there to open a file with Ruby and process the file contents? I don't know for sure, but here are two different ways to do it.

Ruby file processing, version 1

First, I'll use Ruby and the File.open method to open a file and process its contents, like this:

How to show Ruby class documentation from the command line

For some reason I can never remember the command to show Ruby documentation (the rdoc stuff that is a lot like Javadoc) from the command line, so this blog post is especially for me ...

To show Ruby rdoc documentation from the command line, just use the ri command, like this:

Ruby - Variable length argument lists with Ruby

Ruby FAQ: How do I create a variable length argument list in a Ruby method?

One thing I really dig about Ruby is that I can create methods and functions that support variable-length argument lists. It's not something you need all the time, but it sure is nice to have it when you need it.

Here's how you create and then call a Ruby function/method that can take a variable number of arguments:

A Ruby current date and time example

Ruby current date/time FAQ: How can I determine the current date and time in a Ruby script?

You can create a Time object in Ruby to represent the current date/time by calling the Time.now method, like this:

A Ruby write to file example

Ruby file FAQ: How do I write to a file in Ruby?

Writing to a file with Ruby

Many times when you're working with Ruby scripts you need to be able to write text information to a file. Writing text to a file with Ruby is reasonably straightforward. Just like many other languages, you need to open the file in "write" mode, write your data, and then close the file.

Ruby NameError: uninitialized constant error message

Problem: When working with some Ruby code in an irb session, I just got the following error message:

NameError: uninitialized constant Tempfile

Here's a snippet of my irb session where this error occurred:

>> tmp = Tempfile.new
NameError: uninitialized constant Tempfile
	from (irb):1

Solution

This "NameError: uninitialized constant" looks intimidating, but it's actually not a big deal: I just forgot to require the tempfile package.

How to loop through each byte in a Ruby String

Problem: You need to loop through each character in a Ruby String, and get the byte value of the character as you iterate through the characters in the String.

Solution: Just use the each_byte method of the String class to loop through each byte in your String, like this:

a = 'hello, world'
a.each_byte { |c|
  puts c
}

This results in the following output:

How to loop through each character in a Ruby String

Ruby character/string FAQ: How can I loop through each character in a Ruby String, and perform some operation on each character?

I'm currently using Ruby 1.8.6, and you can use the Ruby each_char method if you'll first require the jcode module. To be clear, this code will not work in Ruby 1.8.6:

a = 'hello, world'
a.each_char { |c|
  puts c
}

In fact it results in the following error:

A Ruby substring example

Ruby substring FAQ: I can't find a Ruby substring method, how can I tell if one Ruby string contains another string?

Solution: I expected the Ruby String class to include a method named substring, but it doesn't. My personal disappointment aside, it does have a method named include? that works the way I expected it to. Here's a simple example, using the irbenvironment to perform a few tests.

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.

Ruby glob example - preview many image by creating a single HTML file

Every once in a while someone will give me a large collection of images (JPG, PNG, GIF) that I need to either look at or print. On the Mac (and recent versions of Windows) there are now tools to look at a group of images, but I still like a little program that I wrote.

I wrote this program in Ruby, and what it lets you do is combine all of your pictures into one web page. Then, with the use of CSS, the HTML document you create lets you print each image on a separate page.

Ruby DB2 ODBC - how to access a DB2 database on an As/400 or iSeries

Here's a quick look at a "Ruby DB2" program I created to read data from a DB2 database table on an AS/400 (iSeries) DB2 database.

My Ruby DB2 ODBC example program

First, here's the example Ruby program that accesses the As400 DB2 database, with a few names changed to protect the innocent:

Ruby MySQL DBI example

No real introduction here today, just some sample Ruby code I want to have out here so I can remember some syntax for using Ruby to connect to a MySQL database.

In particular I am also using the here document syntax to simplify the writing and reading of a long database query.

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.)