From: Dave Thomas Date: 2001-11-28T23:48:00+09:00 Subject: [ruby-talk:26803] Re: String#== : Why not error with different type? furufuru@ccsr.u-tokyo.ac.jp (Ryo Furue) writes: > I've considered the problem hard and I think I understand. I think > the earlier example Matz gave > > ary.select{|x| x == "foo"} > > is an answer. This does have a danger, which I described earlier. > But, design 1 would complicate this much, as he said. And this > (perhaps) outweighs the danger. So I'm (perhaps :) convinced. Think of this in human terms. If I point at a cow, and ask you "is this a car?", do you do the Star Trek thing, screaming "this does not compute, this does not compute" until your head explodes? Or do you look at me calmly, say "no", and wonder if perhaps my medication is wearing off. It's the same with Ruby. if a_cow == a_car puts "act surprised" end Remember, in Ruby, classes aren't types, so it would be pretty hard to implement equality which used type checks. > Finally, I'd like to find a way to avoid the danger. Could someone > tell me what "the unit test" is? Is it RubyUnit? Where can I find a > documentation? I searched the Ruby-talk archive, but there too many > articles that mention RubyUnit... Unit testing is a philosophy of testing your code in small chunks, normally writing tests as you go along. You use unit tests to investigate and verify the internal behavior of code, while you'd use functional or acceptance tests to investigate the code's overall behavior. Typically you'd write one or more tests for each non-trivial method that you write. For example, here's some lines from the unit test for a routine that (among other things) expands tabs: def test_tabs str = "hello\n dave" assert_equal(str, basic_conv(str)) str = "hello\n\tdave" assert_equal("hello\n dave", basic_conv(str)) str = "hello\n \tdave" assert_equal("hello\n dave", basic_conv(str)) To make writing unit tests easier, you can use a framework. Runy has two good frameworks, RubyUnit and Lapidary. The code above uses the assert_ methods of RubyUnit. Dave