I’d like to introduce (and thank) another sponsor of Mac Developer Tips: Give Good Food to your Mac is a two week promotion, coinciding with the kickoff of the World Wide Mac Developer Conference, that is offering an intriguing assortment of tools and utilities for developers. From database tools such as DBVisualizer to REALBasic cross-platform IDE to SourceGaurdian PHP encoder, there is assortment of interesting software.

To see what Give Good Food to Your Mac has to offer, click on the banner ad to the right or one of the links in this post (no, I am not paid on a per-click basis :) and spend a few minutes to check out some of the tools and utilities. There are discounts from 20 to 50% while the kitchen is open between the 9th and the 23rd of June 2008.

Here’s a quote from the creators of the promotion that sheds a little light into the name and intention:

No junk food, just healthy, tasty and innovative products. And because we are speaking ‘haute cuisine’ everybody gets to create their own combination of titles matching their own and unique taste.

I’ve been searching for an “ergonomic” keyboard recently and figured I owed it to myself to give one of the Microsoft split keyboards a go.

The keyboard is fine, if not unusual. I find myself spending a fair amount of time looking at the keys as I type since my index fingers have typically done this goofy crossing-over each other reaching for keys that should of been handled by another finger (alas, the split keyboard idea). If nothing else, my typing is much slower and more methodical. The jury is still out on whether this is the best solution of me to ease some of the stress of typing…

What I find rathering amusing is that MS pops up the following warning during the install process:

If you’ve spent any amount of time working on an MS platform, or even if not and you’ve read about the frustrations, you’ll understand the irony behind this dialog and why as a Mac user, it’s amusing.

For what it’s worth, I’ve decided to make Friday’s posts things that are not as much tips as something more on the humorous side. If you have ideas and suggestions, drop me a note.

The ability to treat a string as a file can often be quite handy. Generally this most useful when classes/methods expect a file object as an input type. With Ruby, you can use the StringIO library to use a string when a file is expected, thus allowing operations such as read, write, rewind, etc.

In the lines below, the code after the #=> shows the results when executing the script. Follow the code to see several examples of a string using operations that are typically reserved for files.

require 'stringio'
str = StringIO.new %{This is a test of a string as a file. \r\n
                     And this could be another line in the file}

# Get a line
str.gets # => "This is a test of a string as a file. \r\n"

# Get the next 18 charaters
str.read(18) # => " And this could be"

# Seek to new position and read 7 more characters
str.pos = 59  # => 59
str.read(7) # => "another"

# Rewind the file and overwrite some of the existing text
str.write("Here's how to use")

# Rewind again and output the new contents
str.read # => "Here's how to use a string as a file.
     \r\n And this could be another line in the file"
str.eof? # => true

Any easy way to work with a string in a method that is expecting a file is to create a new StringIO object and pass the result to the method requiring a file type. For example:

  some_method(StringIO.new("Your string here"))

Although an array within Ruby supports all the operations you need for working with a stack (push, pop, etc), the problem is, Ruby does not offer a “formal” stack object and therefore the flexibility of the Ruby array can present problems. For instance, even if you intend to treat an array as a stack, nothing will stop you (or another developer using your code) from inserting or deleting anywhere within the stack.

We can work around this by creating a simple Stack class as follows:

class Stack

  def initialize
   @the_stack = []

  def push(item)
    @the_stack.push item

  def pop

  def count

Here is how you might use the stack:

  stack = Stack.new

If need be, you could also add a few convenience methods such as clearing the stack or looking at the last element (without popping it off the stack):

  def clear

  def look

Give this a try if you need to work with an array as a stack and want to play within the rules of how a stack is implemented as a traditional data structure.