From: "Aleksi Niemelä" Date: 2000-08-13T14:23:53+02:00 Subject: [ruby-talk:04450] Few random notes Here're few random notes I've written down for myself. Maybe some of them have something good behind them, so you maybe want to consider them. - Aleksi =begin Something about hashes: Hash#[]= shouldn't delete when said hash[key]=nil . At least I should be able to select the behaviour. Hash#sort could have calling form with block. There should be appending version of Hash#invert and update, so that ruby -e'a={"a"=>"c", "b"=>"c"}.invert; p a' {"c"=>"b"} would say {"c" => ["a", "b"]}. =end =begin MatchData is not data by itself, it points to $1, $2,... So this doesn't work: ruby -e's="foo bar"; m1=/bar/.match s; m2=/foo/.match s; p m1[0...1];' ["foo"] But this does: ruby -e's="foo bar"; m1=/bar/.match(s).dup; m2=/foo/.match(s).dup; p m1[0...1];' ["bar"] Except that it does not: ruby -e's="foo bar"; m1=/bar/.match(s).dup; m2=/for/.match(s).dup; p m1[0...1];' /tmp/rbREtzJK:1:in `clone': can't clone nil (TypeError) from /tmp/rbREtzJK:1:in `dup' from /tmp/rbREtzJK:1 So working nicely with regexps is a little bit tedious. It wouldn't have to be (but it would be slower with autodupping). =end =begin a, b = [1,2] # is very different from a, b, c = [1, 2], 3 # where a=[1,2], b=3, c=nil (a, b), c = [1, 2], 3 # correct form There's no bug here, I guess, just a note that one have to be careful. =end =begin Emacs notes: - indentation: - when or regexp when %r|^GET\s+/setup(.*)| parameters = $1 # two chars too much print standard_setup =end =begin ruby -e 'class Kernel; def foo; end; end;' 1: Kernel is not a class (TypeError) How about reporting what it is (module in this case)? =end =begin RubyUnit assertions should be added to object too (on request), so instead of assert_kind_of(key, Array) one should say key.assert_kind_of Array And actually my first example is wrong as it should be assert_kind_of(Array, key) and is small gotcha for me. Learnable anyway. (Should we consider assert_kind_of! as a exceptional and extreme case, as we actually want shout it out! Key just *have to be* Array! :) Testsuite should have alias add for add_test. =end =begin if data.something? data.then_proceed else data.something_else_perhaps? data.should_fail? end Following passes parser easily probably just as wanted, maybe it's just a gotcha. Gotcha is that the code looks like and should have been using 'elsif'. Maybe we should force 'term' if 'else' clause has some other code on the same line? Or warning perhaps. =end