From: Einar Boson Date: 2009-01-27T11:11:55+09:00 Subject: Conventions in ruby and the principle of least surprise Hi guys, and hi Matz. I have a few questions about ruby syntax and library that so far has had me surprised. I know Matz doesn't say that ruby shouldn't surprise me, but rather not surprise him but still, i would like to hear the reasoning behind a few weird things: do..end vs. {} Say I have this function: def save_block name, &b @blocks ||= {} @blocks[name] = b end Now this is fine: save_block :say_hello do puts "hello!" end but not this: save_block :say_hello { puts "hello!" } I find that rather annoying but I guess it's to be able to write things like save_block ["name1_long", "name2_long", "short"].find {|n| n.size <= 5}.to_sym do puts "hello!" end Personally I would rather have had to use parentheses to bind the block to find, but I realize that it's hardly up to me :) Anyways. More serious, in my oppinion, is the fact that Array#insert and String#insert are destructive without having an exclamation mark. Why is that? I really do not expect them to change their owner. >> hi = "hello world" => "hello world" >> puts hi hello world => nil >> puts hi.insert(6, "surprising ") hello surprising world => nil >> puts hi hello surprising world => nil seriously, wtf? Why not `insert` and `insert!` that behave as expected? Before I realized this I had a hard time tracking down some unexpected behaviour. -- Posted via http://www.ruby-forum.com/.