Code adapted for Ruby.