Posts in the “ruby” category

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.

Solution

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.

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.

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:

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:

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.

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.

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:

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:

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:

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.

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

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.

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.

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:

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.

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:

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