#30 Recognise Ruby Foo::Bar style class names

open
None
5
2006-09-21
2005-06-02
Ciaran McCreesh
No

Ruby classes can be declared like 'class Foo::Bar'.
Currently this gives a class name of 'Foo'. This patch
allows colons to be included in class and module names.

Discussion

  • Darren Hiebert
    Darren Hiebert
    2006-09-21

    • assigned_to: nobody --> elliotth
     
  • Elliott Hughes
    Elliott Hughes
    2007-05-27

    Logged In: YES
    user_id=1127237
    Originator: NO

    i think the submitter is only half correct. this example:

    #!/usr/bin/ruby -w
    class Foo::Bar
    def initialize()
    $stderr.puts("Foo:Bar")
    end
    end
    Foo::Bar.new()

    gives this error:

    $ ./x.rb
    ./x.rb:2: uninitialized constant Foo (NameError)

    and using a single ":" gives this error:

    $ ./x.rb
    ./x.rb:2: syntax error, unexpected ':', expecting '<' or '\n' or ';'
    class Foo:Bar
    ^

    you can use :: as a C++-like scope resolution operator, including in definitions:

    #!/usr/bin/ruby -w
    module Foo
    end
    class Foo::Bar
    def initialize()
    $stderr.puts("Foo:Bar")
    end
    end
    Foo::Bar.new()

    but the tag there isn't Foo::Bar, it's Bar in the scope of Foo. accepting ":" as part of an identifier would be wrong. we need to be more intelligent when parsing definitions and correctly enter/exit the enforced scope instead.