Menu

#1166 [ruby] mixed case module names not handled correctly

None
open
5
2023-08-08
2011-05-14
No

%module OpenBabel
gives rise to a .cpp file with
#define SWIG_init Init_OpenBabel
#define SWIG_name "OpenBabel"

When you try 'require openbabel', it complains:
" undefined symbol: Init_openbabel"

The ruby convention would be: 'require openbabel', and then "OpenBabel::OBObject.new"

Use of %openbabel imports fine, but the resulting module name has the wrong case, "Openbabel::OBObject.new"

Discussion

  • Olly Betts

    Olly Betts - 2011-05-24
    • summary: mixed case module names for Ruby not handled correctly --> [ruby] mixed case module names not handled correctly
     
  • William Fulton

    William Fulton - 2012-12-10
    • assigned_to: gga73 --> kkaempf
     
  • Olly Betts

    Olly Betts - 2017-10-08

    Still the case with current git master.

    FWIW, you can run swig -ruby -initname openbabel to achieve what you want.

    Perhaps the default should be the module name lowercased instead of the module name as-is, but that needs input from someone who knows more about Ruby than I do.

    Also, SWIG_init and SWIG_name aren't actually used in Ruby - it generates the name of the Init_OpenBabel function directly, rather than calling it SWIG_init like some other languages do.

     
  • Olly Betts

    Olly Betts - 2017-10-08

    This would be the change I think (untested):

    diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx
    index 25988d6f..3bc16f6c 100644
    --- a/Source/Modules/ruby.cxx
    +++ b/Source/Modules/ruby.cxx
    @@ -1285,7 +1285,7 @@ public:
            }
            if (last) {
              if (feature == 0) {
    -           feature = Copy(last);
    +           feature = Swig_string_lower(last);
              }
              (Char(last))[0] = (char)toupper((Char(last))[0]);
              modvar = NewStringf("m%s", last);
    
     
  • Olly Betts

    Olly Betts - 2022-03-16

    Still seems to be the case with current git master.

     
  • Olly Betts

    Olly Betts - 2023-08-08

    https://github.com/rubocop/ruby-style-guide#snake-case-files adds evidence that what SWIG calls the "init name" ought to be lower case. According to that it perhaps ought to be open_babel here rather than openbabel for %module OpenBabel - SWIG does have CamelCase to snake_case conversion code so we could easily do that, and there's always -initname openbabel if that's what you really want.

    I think this should just apply to the auto-generated default, and an explicit -initname OpenBabel should allow that people who really want require 'OpenBabel' to still have it.

    Currently there are some examples in the manual which violate this which we should update to use more conventional names too:

    $ git grep "require *'[A-Z]"
    Doc/Manual/Ruby.html:require 'Example'
    Doc/Manual/Ruby.html:irb(main):001:0&gt; <b>require 'Example'</b>
    Doc/Manual/Ruby.html:irb(main):001:0&gt; <b>require 'Example'</b>
    Doc/Manual/Ruby.html:irb(main):001:0&gt; <b>require 'Example'</b>
    Doc/Manual/Ruby.html:irb(main):001:0&gt; <b>require 'Example'</b>
    Doc/Manual/Ruby.html:<pre>require 'Example'
    Doc/Manual/Ruby.html:<pre>require 'Argv'
    
     
  • Olly Betts

    Olly Betts - 2023-08-08
    • labels: ruby --> ruby, patch
    • Group: -->
     

Log in to post a comment.