From: gwtmp01@... Date: 2007-01-11T23:40:52+09:00 Subject: Re: seeking advice on Ruby code style On Jan 11, 2007, at 8:49 AM, Alexandru E. Ungur wrote: > My question is, which is more elegant, more... rubyful ? > I like that toggle_word1 is almost like plain english, especially > that "case" construct, but I like in toggle_word2 that the same "case" > construct was replaced with a single line of code, well with two > actually. However perhaps toggle_word2 is a little too much > code golfing...? I think in both cases you are repeating too much work that could be done ahead of time. How about: class String base = %w[on off yes no true false] all = base all += base.map {|x| x.upcase } all += base.map {|x| x.capitalize } t = Hash[*all] TOGGLE = t.merge t.invert def toggle_word3 TOGGLE[self] || TOGGLE[downcase] end end if __FILE__ == $0 require 'test/unit' class My_Test < Test::Unit::TestCase def test_toggle_word3 assert_equal('off', 'on'.toggle_word3) assert_equal('Off', 'On'.toggle_word3) assert_equal('YES', 'NO'.toggle_word3) assert_equal('yes', 'nO'.toggle_word3) assert_equal('false', 'true'.toggle_word3) assert_equal(nil, 'unknown'.toggle_word3) end end end Gary Wright