From: Daniel Brockman Date: 2005-06-12T18:10:25+09:00 Subject: Hyphens vs. underscores Hi list, I just wrote this, a simple preprocessor that converts between conventional Ruby-style identifiers and Lisp/Dylan/XML-style ones. For example, @user-server = Nmdc::UserServer.new( :external-address => options.external-address, :external-port => options.external-port) @user-server.on-signal :opened do if @user-server.address.zero? puts "Listening to port #{@user-server.port}, all interfaces." else puts "Listening to #{@user-server.address}:#{@user-server.port}." end Notice how the hyphens work to restore the center of gravity of each line to the typographical center of the line. I think this makes Ruby code look a little cleaner. Here's a slightly hairier example: def on-signal *args, &block add-signal-handler *args, &block end def remove-signal-handler name, handler __maybe-initialize-signal-emitter @signal-handlers[name].delete handler end def signal name, *args, &block __maybe-initialize-signal-emitter __send__ "on_#{name}", *args, &block @signal-handlers[name].each { |x| x.call *args, &block } end Note how I still use underscores for prefixing and suffixing, which I have always thought looks nice. It communicates ``internal'' nicely. Obviously, the preprocessor cannot handle quoted code (it would not be possible to do so statically). In fact, as I didn't follow any formal grammar or anything when writing the scanner, there will likely be many cases that it should be able to handle correctly but doesn't. Anyway, I just wanted to see if I could stir up a reaction here, and maybe get matz to comment on the possibility of getting this syntax accepted into a future version (Ruby 2?). :-) I don't think it's really that bad with regards to backwards compatibility, as it only breaks code that looks roughly like this, def moomin foo @gizmo.frob(@bar-foo) end which I for some reason think is pretty rare. It doesn't break this, def moomin foo @gizmo.frob(-foo) end nor this, def moomin foo @gizmo.frob(10-foo) end but it _does_ break this, def moomin foo @gizmo.frob(foo-10) end so the 10-foo case had better at least provoke a warning. (This syntax would provide a good reason to deprecate most usages of binary operators that are too low on whitespace (e.g., foo+bar). I think doing so would be liberating to the syntax in the long run, but I'm not sure that it would be good idea; it'd probably be likely to annoy people. I certainly don't feel strongly about it.) The GLib signal and property systems are the only precedents for allowing both `foo-bar' and `foo_bar' as synonyms that I know of. I'd be interested to hear about any other such examples. Thanks, -- Daniel Brockman ``Why fix an old bug if you can write three new ones in the same time?'' --- David Kastrup (on emacs-devel)