Hyphens vs. underscores

Discussion in 'Ruby' started by Daniel Brockman, Jun 12, 2005.

  1. Hi list,

    I just wrote this,

    <http://www.brockman.se/software/hyphen-ruby/>

    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 :eek:pened 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)
    Daniel Brockman, Jun 12, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ibiza
    Replies:
    5
    Views:
    1,153
    S. Justin Gengo [MCP]
    Feb 9, 2006
  2. spaghetti

    Re: "Soft" hyphens

    spaghetti, Jun 24, 2003, in forum: HTML
    Replies:
    0
    Views:
    910
    spaghetti
    Jun 24, 2003
  3. Jukka K. Korpela

    Re: "Soft" hyphens

    Jukka K. Korpela, Jun 24, 2003, in forum: HTML
    Replies:
    0
    Views:
    921
    Jukka K. Korpela
    Jun 24, 2003
  4. Augustus

    Re: "Soft" hyphens

    Augustus, Jun 24, 2003, in forum: HTML
    Replies:
    4
    Views:
    882
    Anonymous Joe
    Jun 25, 2003
  5. James Harris
    Replies:
    46
    Views:
    999
    Ronald Landheer-Cieslak
    Jul 23, 2004
Loading...

Share This Page