On Thu, Jan 11, 2007 at 10:49:29PM +0900, Alexandru E. Ungur wrote:
> I recently recently released my first vim script, nothing big, it
> just toggles words between true/false, on/off, etc. You can find
> more here http://vim.sourceforge.net/scripts/script.php?script_id=1748
> if you want.
[...]

Here's a cleaner implementation (assuming I understand it correctly):

class String
  PAIRS = Hash[*%w(on off yes no true false)]
  PAIRS.to_a.each { |v,k|
    v.freeze
    k.freeze
    PAIRS[k] = v
  }
  PAIRS.freeze

  def toggle_word
    return self unless antiword = PAIRS[downcase]
    case self
    when upcase
      antiword.upcase
    when downcase
      antiword.downcase
    when capitalize
      antiword.capitalize
    else
      antiword
    end
  end

end

Note that I am avoiding class variables (which are almost always the wrong
choice, see
http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html
) and using a constant instead. The freezing is optional, but I like to
freeze what my constants point to. Notice that I am reversing all the
entries in the hash so it's easy to toggle both ways. I kept the case
statement from toggle_word1 over the golfing in toggle_word2, but I'll
point out that Ruby only requires a return when short-circuiting.

> Thank you in advance for your comments,
> Have a nice day everyone,
> Alex
--Greg