Upgrading plugins to Ruby 2.0 for SketchUp 2014

Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby marksup » Mon Mar 03, 2014 10:31 am

Hopefully Ruby experts can comment on and append to this topic to create a useful overview of what has changed and needs to be recoded. (Or perhaps there is a useful website page somewhere that already provides this information?)

Syntax...
Support for retry within a loop has been dropped.
Support for same line assignment for case - when (using a colon) has been dropped.

RUBY_PLATFORM... Note that (for windows) this variable value has changed.

Operation...
"A"[0] no longer returns the ANSI integer. Use "A".unpack("c")[0] instead.
["A","B","C"].to_s is now ineffectual. Use ["A","B","C"].join("") instead.

Character coding...
I am currently still trying to solve many "Invalid multibyte char (UTF-8)" errors, for currency symbols, 2, 3 and any foreign characters. I have found a $KCODE variable (previously set to UTF8), but which now unfortunately is apparently no longer supported. More expert advice on this issue would be very helpful.
0

marksup 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby jolran » Mon Mar 03, 2014 11:15 am

This topic should be made sticky and simply called "Upgrading plugins to Ruby 2.0", in my opinion.
1
User avatar
jolran 
PluginStore Author
PluginStore Author
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby TIG » Mon Mar 03, 2014 11:23 am

ANSI encoded script files will break Ruby2, but were 'tolerated' by earlier versions...
Use Notepad++ and ensure the rb is saved encoded as UTF8-without-BOM.
Redo any now incorrect m² etc to suit UTF [just type/paste the 'special character'].
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Mon Mar 03, 2014 2:52 pm

marksup wrote:I am currently still trying to solve many "Invalid multibyte char (UTF-8)" errors, for currency symbols, 2, 3 and any foreign characters. I have found a $KCODE variable (previously set to UTF8), but which now unfortunately is apparently no longer supported. More expert advice on this issue would be very helpful.

Save your RB files in UTF-8 encoding. For compatibility with older Ruby save without the BOM mark.

The "Invalid multibyte char" is probably due to files being saved in ANSI format. The bytes in ANSI from 127-255 is not compatible with UTF-8 where they represent control characters.
0
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby marksup » Mon Mar 03, 2014 3:43 pm

Upgrade addition...
Hash-array index has been replaced by key...
key = ( Sketchup.version.to_i > 13 ) ? hash-array.key(value) : hash-array.index(value)

re: Encoding...
I have tried notepad++ Encoding convert to UTF-8 without BOM but now get...
Error: #<Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT>

Also, what might a (newly supported) unicode string look like?!
0

marksup 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby TIG » Mon Mar 03, 2014 3:47 pm

Once you have converted your script file to UFT8-without-BOM and saved [don't just view it with that encoding - it needs converting!]... then you need to replace earlier special characters [if any] in it that are no longer compatible.
Simply retype the desired text and it should encode properly e.g.
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Mon Mar 03, 2014 4:46 pm

marksup wrote:Upgrade addition...
Hash-array index has been replaced by key...
key = ( Sketchup.version.to_i > 13 ) ? hash-array.key(value) : hash-array.index(value)

Easier to just use key as that works on both versions. Less code branching.

marksup wrote:re: Encoding...
I have tried notepad++ Encoding convert to UTF-8 without BOM but now get...
Error: #<Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT>

Can you provide context around this error.

marksup wrote:Also, what might a (newly supported) unicode string look like?!

It should look like anything different, but under the hood, all characters that is outside the ASCII range (0-127) will be multi-byte characters.
0
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby marksup » Mon Mar 03, 2014 5:46 pm

Hi TT,
Are you sure about key (and not key?) being valid previously...
ha = {"A"=>"ABC"}
ha.key("ABC")
Error: #<NoMethodError: undefined method `key' for {"A"=>"ABC"}:Hash>
(eval)
0

marksup 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Mon Mar 03, 2014 6:06 pm

marksup wrote:Hi TT,
Are you sure about key (and not key?) being valid previously...
ha = {"A"=>"ABC"}
ha.key("ABC")
Error: #<NoMethodError: undefined method `key' for {"A"=>"ABC"}:Hash>
(eval)

Ah! My bad, I misread that. I stand corrected.
0
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby Brighter3D » Tue Mar 04, 2014 1:43 pm

Win32API.so seems to no longer works on SU2014, instead we should use build in Win32API.rb.

When passing
Sketchup.active_model.skpdoc
pointer,
I've got error:

Error: #<TypeError: no implicit conversion of Fixnum into String>
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:23:in `pack'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:23:in `block in call'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `each'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `each_with_index'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `call'
C:/Users/l/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/BrighterSU/BrighterSU.rb:68:in `mydll_render'
C:/Users/l/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/BrighterSU/BrighterSU.rb:248:in `block in InitToolbar'
-e:1:in `call'

funciton definition:
BRrender = Win32API.new(mydll,"RenderScene",["P","P","I","I","I"],"I")

function call:
BRrender.call(Sketchup.active_model.skpdoc,Sketchup.active_model.path,width.to_i,height.to_i,nFromSett.to_i)

Is it because of SU 64bit version?
Does anyone have similar problem or anyone succeed to pass
Sketchup.active_model.skpdoc pointer into c++ dll with SU2014?
0

Brighter3D 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Tue Mar 04, 2014 2:17 pm

Brighter3D wrote:When passing
Sketchup.active_model.skpdoc
pointer,
I've got error:

Error: #<TypeError: no implicit conversion of Fixnum into String>
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:23:in `pack'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:23:in `block in call'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `each'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `each_with_index'
C:/Program Files (x86)/SketchUp/SketchUp 2014/Tools/RubyStdLib/Win32API.rb:22:in `call'
C:/Users/l/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/BrighterSU/BrighterSU.rb:68:in `mydll_render'
C:/Users/l/AppData/Roaming/SketchUp/SketchUp 2014/SketchUp/Plugins/BrighterSU/BrighterSU.rb:248:in `block in InitToolbar'
-e:1:in `call'

funciton definition:
BRrender = Win32API.new(mydll,"RenderScene",["P","P","I","I","I"],"I")

function call:
BRrender.call(Sketchup.active_model.skpdoc,Sketchup.active_model.path,width.to_i,height.to_i,nFromSett.to_i)

Is it because of SU 64bit version?
Does anyone have similar problem or anyone succeed to pass
Sketchup.active_model.skpdoc pointer into c++ dll with SU2014?


SketchUp is still 32bit.

The error you are getting is from the Win32API module. Seems to not like it that you feed it integer. It's expecting strings. Maybe you'll get some hints if you dig into Win32API.rb at the line number that raised the error. This Win32API compatibility shim might not be 100% compatible with the old Ruby 1.8. I'm not sure how much it changed between Ruby 1.8 and Ruby 2.0.
1
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby Brighter3D » Tue Mar 04, 2014 7:27 pm

SketchUp is still 32bit.


What a pity, I thought this version is 64bit already :(.
There is still no possibility to load 64bit's dlls :(.

The error you are getting is from the Win32API module. Seems to not like it that you feed it integer. It's expecting strings. Maybe you'll get some hints if you dig into Win32API.rb at the line number that raised the error. This Win32API compatibility shim might not be 100% compatible with the old Ruby 1.8. I'm not sure how much it changed between Ruby 1.8 and Ruby 2.0.


Thank you, you put my thoughts into right direction.

changing
BRrender = Win32API.new(mydll,"RenderScene",["P","P","I","I","I"],"I")
into
BRrender = Win32API.new(mydll,"RenderScene",["I","P","I","I","I"],"I")

solves the problem.
Looks like "P" is char* now, and "I" can be int or void*...
0

Brighter3D 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Wed Mar 05, 2014 12:25 pm

Brighter3D wrote:What a pity, I thought this version is 64bit already :(.
There is still no possibility to load 64bit's dlls :(.

Yea unfortunately it's not. I know it's a pain for render engines. The workaround would be to use a 64bit helper process to host the render engine.

Brighter3D wrote:
The error you are getting is from the Win32API module. Seems to not like it that you feed it integer. It's expecting strings. Maybe you'll get some hints if you dig into Win32API.rb at the line number that raised the error. This Win32API compatibility shim might not be 100% compatible with the old Ruby 1.8. I'm not sure how much it changed between Ruby 1.8 and Ruby 2.0.


Thank you, you put my thoughts into right direction.

changing
BRrender = Win32API.new(mydll,"RenderScene",["P","P","I","I","I"],"I")
into
BRrender = Win32API.new(mydll,"RenderScene",["I","P","I","I","I"],"I")

solves the problem.
Looks like "P" is char* now, and "I" can be int or void*...

Oh good! Glad that worked. I think there has been some tweaks made between Ruby 1.8 and 1.9 in that respect.
0
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby marksup » Tue Mar 18, 2014 1:37 pm

I have written a CSV format file SketchUp Importer which includes the following lines...

File.foreach(csv_filepath) do |file_data|
next if file_data.match(/\A#/)

The CSV file includes the following comment line: # Room-Name, Floor Nr, Area m², Length

Sketchup 2014 Bugsplats on processing the CSV file (at the match statement) if it is not UTF-8 encoded and includes the squared character.

Does this need to be fixed by SketchUp and/or is there anything I can do to detect/avoid the issue (since I can not control either the selected CSV file content or its encoding)?

These UTF-8 errors are insidious, and a definitive guide to their avoidance would be very helpful. What have other Ruby programmers been doing (prior to SketchUp 2014) to adjust to the new Ruby version?, are there no existing resources?
0

marksup 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby tt_su » Tue Mar 18, 2014 2:03 pm

marksup wrote:Does this need to be fixed by SketchUp and/or is there anything I can do to detect/avoid the issue (since I can not control either the selected CSV file content or its encoding)?

Bugsplats should not happen and is something we should fix. Have you submitted the splats? With any information we can use to look it up?

marksup wrote:These UTF-8 errors are insidious, and a definitive guide to their avoidance would be very helpful. What have other Ruby programmers been doing (prior to SketchUp 2014) to adjust to the new Ruby version?, are there no existing resources?

If you search on Ruby in general, without the context of SketchUp, you find a number of articles and forum posts with people migrating from Ruby 1.8 to 1.9/2.0.

The bugsplat here is troublesome, because it should just raise an error. If it had raised an error you would have been able to try again with a different encoding. Now, I haven't read arbitrary files with ruby yet, but I would have thought there would be a methods in the Ruby StdLib to detect encoding of a given string/file?

Back to the bugsplat, can you provide a small snippet that reads a file and a file that will cause it to crash? Smallest possible snippet to reproduce this?
0
User avatar
tt_su 
SketchUp Team
SketchUp Team
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby TIG » Tue Mar 18, 2014 2:16 pm

If the .CSV file is not in UTF8-without-BOM encoding, but say ANSI, and also since it may include characters like 'm²', these both might cause you issues.
It's easy enough to [re]encode any file appropriately using Notepad++.exe...
However if the 'm²' were typed in say Unicode, then it might then be converted to a strange looking character combo when re-encoded.
In that case you need to retype the character[s] and it should convert automatically to display in UTF8 encoding...
Save and retry.

Of course, the .RB file itself must of course be properly encoded as UTF8-without-BOM to be compatible with v2014's Ruby2.0, and remember that any special characters [like 'm²'] which you had in any earlier encoding might have to be corrected if already UTF8 compliant... as outlined above...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby marksup » Tue Mar 18, 2014 2:37 pm

Notepad++ is great, but customers of plugins can not be expected to use it to investigate and correct the encoding of their data files.

Is the encoding of a scrambled Ruby file significant? - since a scrambled UTF-8 .rb is reported by Notepad++ as ANSI.
0

marksup 
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby TIG » Tue Mar 18, 2014 2:53 pm

It is unfortunate that the encoding of a PC's plain Notepad file is in ANSI.
However a txt/csv file made in Excel or through a Ruby script should be encoded as compatible UFT8-without-BOM...

If you have made the RBS from an incorrectly encoded RB it will fail in v2014.
If you make the RB correctly encoded than encrypt the RBS from that is should be compatible.
What Notepad++ sees the contents as is somewhat academic as isn't it a binary file ?

Files encoded as UFT8-without-BOM should be compatible with v2014 Ruby2.0 AND all earlier versions of SketchUp and their Ruby version...

You could try trapping for the Sketchup.version >= 14 then re-encoding the string got from any data file.
You should still be able to 'read' the contents of an ANSI encoded data file [or any type of data file - even binary is 'readable'], but to then use that string in Ruby2.0 you probably need to force some recoding...
Code: Select all
if Sketchup.version.to_i >= 14
  lines = IO.read(csv_file_path).force_encoding('UTF-8')
else
  lines = IO.read(csv_file_path)
end
lines.split("\n").each{|line|
  ### process the line...
}
I typed this without reference and it is untested, but you get the idea...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

Postby Dan Rathbun » Tue Mar 18, 2014 3:10 pm

Making the first statements of the block:

Code: Select all
if defined?(Encoding)
  file_data.encode!("UTF-8") unless file_data.encoding == Encoding::UTF_8
end


:idea:
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby tt_su » Tue Mar 18, 2014 3:43 pm

    You could try to open the file as ASCII-8bit - then attempt to convert the data afterwards. But in general you need to know what encoding a file is in. You'll see editors often provide this option, even Notepad.

    After reading the file as ASCII-8bit - which is in effect what Ruby 1.8 did - then you could try to change the encoding to UTF-8 - if that fails it's a good chance the file is ANSI encoded in which you can retry with that. (Though there are many variants of ANSI, US-ANSI is most normal one.)


    As for the crashes - as I mentioned, that should not happen. Did you submit those report? This is important for us in order to address the crash.
    0
    User avatar
    tt_su 
    SketchUp Team
    SketchUp Team
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby marksup » Tue Mar 18, 2014 3:56 pm

    Bugsplat report duly filed.
    0

    marksup 
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby tt_su » Tue Mar 18, 2014 5:25 pm

    marksup wrote:Bugsplat report duly filed.

    Keywords we can search for?
    0
    User avatar
    tt_su 
    SketchUp Team
    SketchUp Team
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby marksup » Tue Mar 18, 2014 6:06 pm

    I used the original post above as the scenario description, so... Ruby, Importer, UTF-8 and/or m² should find it.
    0

    marksup 
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby tt_su » Wed Mar 19, 2014 10:40 am

    hm... this crashes deep into the Ruby interpreter...

    Can you provide a small code snippet to reproduce it?
    0
    User avatar
    tt_su 
    SketchUp Team
    SketchUp Team
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby tt_su » Wed Mar 19, 2014 10:44 am

    I see you included some description in the BugSplat, but I'm afraid it's been mangled formatting. Can you attach sample RB file and sample CSV file? That'll ensure we are reproducing 100% correctly.
    0
    User avatar
    tt_su 
    SketchUp Team
    SketchUp Team
     

    Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

    Postby Dan Rathbun » Wed Mar 19, 2014 4:11 pm

    FWIW. Now in Ruby 2.x, you can specify the arguments that will be used for IO.new via IO.open, as the last argument to File.foreach(), which is inherited from IO.foreach().

    Example
    Code: Select all
    File.foreach( csv_filepath, {:mode=>"r", :encoding=>"ASCII:UTF-8"} ) do |file_data|
      # statements
    end


    :idea:
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

      Postby tt_su » Wed Mar 19, 2014 5:09 pm

      Dan Rathbun wrote:"ASCII:UTF-8"

      :?:
      0
      User avatar
      tt_su 
      SketchUp Team
      SketchUp Team
       

      Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

      Postby Dan Rathbun » Wed Mar 19, 2014 7:11 pm

      I also wrote:
      Dan Rathbun wrote:..., you can specify the arguments that will be used for IO.new

      so ...
      http://www.ruby-doc.org/core-2.0.0/IO.html#method-c-new

      It's an example.

      At the console you can get names like this:
      Encoding::ASCII.names
      >> ["US-ASCII", "ASCII", "ANSI_X3.4-1968", "646"]


      Encoding::ASCII_8BIT.names
      >> ["ASCII-8BIT", "BINARY"]


      :P
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

        Postby TIG » Fri Mar 21, 2014 3:30 pm

        I had a weird problem with a txt file using IO.read to get a string...
        Notepad++ said was encoded as ANSI, but Ruby2.0 said was was encoded as UTF-8...
        I could read and parse the string in Ruby1.8, but in Ruby2.0 it sometime 'threw a wobbler' about wrongly encoded characters...

        If the string was all normal ASCII type characters then no issue...
        BUT if the string contained an accented character it caused the 'wobbler'...

        If I re-encoded it in Notepad++ to UTF8-without-BOM - then again no issues with the accents in Ruby2.0 or Ruby1.8.

        A 'puts' for the strings read for the two file encodings showed differences with \E... etc where the accents were.

        In Ruby2.0 I tried to force the encoding to UFT-8, but since it thought it already was in that it failed.
        This was my workaround:

        data = IO.read(@data_file)
        data = data.force_encoding('ISO-8859-1').encode("UTF-8") if defined?(Encoding)
        @uid = data.split('&')[-1].to_s.chomp
        -->
        Gábor

        etc...

        Now in v2014 it reads and parses OK and in earlier versions it works too...
        Forcing the encoding into one that it is never going to be [as in my case anyway] and then back to UTF-8 works...
        I don't know why it thought the encoding was NOT in ANSI but...

        Incidentally - this also uncovered another oddity...
        In Ruby1.8 if you use the File.new to make a txt file and write an ASCII string it is reported as a UTF8... encoded file by Notepad++
        BUT if the string contains an accented character the file encoding is reported as ANSI.
        Since Ruby1.8 can cope with either encoding when parsing strings etc it causes no issues, but if you have such a txt file, without the above double encoding workaround it causes issues with Ruby2.0...
        0
        TIG
        User avatar
        TIG 
        Global Moderator
         

        Re: Upgrading plugins to Ruby 2.0 for SketchUp 2014

        Postby marksup » Fri Mar 21, 2014 3:53 pm

        TIG - THANK YOU!

        I was working on the UTF-8 errors issue when your last post displayed. I can confirm that...

        File.foreach(csv_filepath) do |file_data|
        file_data.force_encoding('ISO-8859-1').encode("UTF-8") if defined?(Encoding)

        Works in both (SketchUp) Ruby releases exactly as you say.
        0

        marksup 
         

        SketchUcation One-Liner Adverts

        by Ad Machine » 5 minutes ago



        Ad Machine 
        Robot
         

        Next


         

        Return to Developers' Forum

        Who is online

        Users browsing this forum: No registered users and 6 guests

        Visit our sponsors: