Ruby and special characters
-
hi,
i get ifc-fields from german cad-programs, which have "ä, Ü, " etc. in the ifc-descriptions.
ho can i handle this in ruby, when i want to check them?
example:
iterate key / value...
if key == "Fläche" then...
saving this the ruby do not work...
thanx
stan -
In the very first line of your Ruby RB file add this.
# encoding; UTF-8
This should ensure your own code is properly formatted.
Additionally, if needed, to force your 'read' ifc data file's text into the correct encoding try this...
text_string_read from_ifc_file.force_encoding("UTF-8")
-
thanx a lot. perfect-:)
-
hi tig,
first : thanx for your help.
but
i am doing something wrong.i placed the utf info at the very first line (above "Module")
` # encoding: UTF-8
module ZF_ifc#########################################################################################
class ZF_BIM_version
#######################################`but i still get
Error:
#<SyntaxError: C:/Users/zdene/AppData/Roaming/SketchUp/SketchUp 2023/SketchUp/Plugins/zf_areal3d_bim/areal3d_bim_prg.rb:6743: invalid multibyte char (UTF-8)>so i cannot search in keys for german letters (ä,ü,ö).
in the key of the hash there is an "ä"
i want to check with ruby, if there is an "ä" in ite key:
elsif tt[0].to_s.include?("Länge")
elsif tt[0].to_s.include?("Länge")
elsif tt[0].to_s.include?("Länge")
i just stick.....
stan
-
#<SyntaxError: C:/Users/zdene/AppData/Roaming/SketchUp/SketchUp 2023/SketchUp/Plugins/zf_areal3d_bim/areal3d_bim_prg.rb:6743: invalid multibyte char (UTF-8)>
Tells you the line where the error is...
Have you tried simply copy/pasting the same string with the ä ü ö characters
into your script, so as to sidestep the issue ?
e.g.elsif tt[0].to_s.include?("Länge")
an alternative format could be
elsif tt[0].to_s=~/Länge/
If the RB is specifically defined as using UTF-8 then it should work...
-
the alternativ format is perfectly working !
thanx a lot for this, i di not know this syntax yet
stan
-
hi tig,
just a small add to this toppic:situation:
i imported a ifc from a german cad with special chatacters "ä,ö,ü..."
in the "key" of the ifc-parameterwe solved the handling for ruby by using ift-8
so far, so good.
now i tried to export this file from skp to ifc
and reimport ( to see, if my attributes are passed correctly).can it be , that sketchup exports the code of the special character in some special way?
in reimport the ifc-key contents the code itself now, not the original special character - somehow...
screenshots show the display in the original file and after the re-import.
i want to try to detect those codes by ruby again to be able to differ between areas, length and so on.
regards
stan
-
What text encoding does the original ifc file use ?
Once you know that you can try to convert the encoding of the final edited string you want to put into that file, encoding it into the appropriate format...
https://www.educative.io/answers/how-to-use-the-encoding-class-in-ruby
https://www.cloudbees.com/blog/how-ruby-string-encoding-benefits-developersThere are lots or resources on the http://www...
-
hi tig,
ok, if it depends on the encoding of the original file imported to skp, i will have to study it.
thanx and merry x-mas!stan
-
@tig said:
In the very first line of your Ruby RB file add this.
# encoding; UTF-8
This should ensure your own code is properly formatted.
This magic comment does not ensure that the code file is saved as UTF-8 w/o BOM.
This must be set in whatever code editor is being used.
Advertisement