Rookie Ruby Mistake

Thursday, October 12, 2006 by Nate Murray.  

I made a rookie mistake today, so I thought I'd share it. Consider the following code:

s = "hello world"
a = ["foo", "bar", s]

other_s = s            # not a copy!
other_s.upcase!

puts a[2]              # => "HELLO WORLD"

Notice that the contents of a were changed. This is because the variable other_s is simply a reference to the same object that is in a. It's such a simple thing to overlook when things are more complicated. It's subtle and that can make it difficult to track down.

The lesson? When you edit an object in place make sure that is what you want!

Labels:

Ruby Debugger

by Nate Murray.  

I just started to learn the ruby debugger. Its pretty helpful in those cases where puts just doesn't cut it. I found a good introductory article on this over at IBM: Debugging Ruby programs 101 (registration required). One of the most annoying things was that there seemed to be a catchpoint on every single exception that was raised. I had to keep hitting c just to get to my first actual breakpoint. I found the solution to this problem over at comp.lang.ruby. Basically, there is a default catchpoint on StandardError so you are prompted at all raise exceptions. What you need to do to fix this is just set a catchpoint for your own type of error. So when you start up simply:

  cat MyOtherError

It will remove the default catchpoint and catch only on MyOtherError.

Another helpful Ruby Debugger tip is to have the debugger load a debug.rc file on startup. Matthias Georgi posted this hack on comp.lang.ruby that could be helpful.

A short hack.
Make a copy of debug.rb and put these lines right after the Context
class.

class Context
  alias original_readline readline
  def readline(prompt, hist)
    @rc_file ||= File.readlines("debug.rc")
    if @rc_file.empty?
      original_readline(prompt, hist)
    else
      @rc_file.shift
    end
  end
end

Now create a file debug.rc with your desired breakpoint:
b 100
b 200

Start the debugger:
ruby -r./debug myscript.rb 

Of course, this isn't a totally clean solution in that you are editing a standard file. Also, I don't think this code would work if the debug.rc file did not exist. Nonetheless, its the easiest method I've seen so far.

Labels:

BackupGem

Tuesday, October 10, 2006 by Nate Murray.  

I've just released the first version of my BackupGem. You can read the manual here: http://tech.natemurray.com/backup/ BackupGem aims to be the easiest and most flexible backup, archive and rotate tool. It’s a beginning-to-end solution for scheduled backups in a clean ruby package that is simple use and powerful when customized. A configuration as simple as:

  set :backup_path, "/var/local/backups/my_old_logs"
  action :content,  :is_contents_of => "/var/my_logs"

Will compress, encrypt, deliver, and rotate the contents of /var/my_logs. But this is just a taste of the power this gem has.

If you are interested the gem is available via:

  gem install backupgem

Or view the manual at: http://tech.natemurray.com/backup/
RubyForge Project page: http://rubyforge.org/projects/backupgem/

Labels: , ,

Who we are:
The Pasadena Ruby Brigade is a group of programmers who are interested in Ruby. We enjoy sharing our ideas and code with anyone who is interested in this wonderful programming language.

Who can join:
Anyone! Just head over to our mailing list and drop us an email.

What we do:
We recently started a project over at RubyForge. This project is a group of programs written and maintained by our members that we thought could be beneficial to the whole community.

Projects

Downloads

Recent Posts

Archives