• Login
sketchucation logo sketchucation
  • Login
🔌 Quick Selection | Try Didier Bur's reworked classic extension that supercharges selections in SketchUp Download

RBeautify - Ruby Code Beautifier

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 4 Posters 1.9k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    Al Hart
    last edited by Al Hart 24 Apr 2011, 21:48

    There are a couple of things I like to do to Ruby code to make it easier to work with:

    1. Use Indents properly to make it easy to follow groups of code

    2. Add #xxx after 'end' to make it easier to remember what they are the end of:

    #e.g. 
        if (a == b)
            c = d
        end#if
    

    (I really wish the Ruby inventors had not decided to use the same word, 'end', for the end of everything. It would make it a lot easier to track down mistakes.

    1. Make blank lines consistent.

    So I borrowed a Ruby Beautifier Script from Paul Lutus, (which did #1), and added logic to it to do #2 and #3.

    I have attached a copy if you want to try it out.

    new version 5/29/2015

    Here is what it does to a ruby script which was already in good shape - bomb.rb by Rick Wilson (A free script I downloaded from Smustard)

    Original on right, RBeautify version on left
    rbeautify.jpg
    (This output is from Araxis Merge - a great program we use to show changes between versions of source code)

    Al Hart

    http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
    IRender nXt from Render Plus

    1 Reply Last reply Reply Quote 0
    • C Offline
      cesaro36
      last edited by 28 May 2015, 02:41

      Hi Al
      I'm trying to use your rbeautify.rb but I get this error:

      RBeautify.beautify_file("c:/rotular.rb")
      Error: #<NoMethodError: undefined method each' for #<String:0x0000000fef1050>> C:/rbeautify.rb:90:in beautify_string'
      C:/rbeautify.rb:194:in beautify_file' <main>:in <main>'
      SketchUp:1:in `eval'

      where rotular.rb is just a generic rb that I'm using as a sample.
      Could you please help me?

      Tks

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 28 May 2015, 12:28

        This is an old script.
        It's not compatible with >= v2014's Ruby 2.
        You can 'fix it' by finding the offending line -
        source.each do |line|
        that is processing the string OK in earlier Ruby's and use something like:
        source**.split**.each do |line|

        IMHO the script is a bit flawed and flaky.
        It uses $ global variables that are frowned on in current coding circles...
        You'd be better off getting Notepad++ [or TextWrangler for MAC] and using its built-in Ruby syntax highlighting and learn to indent your code manually as you go...
        That would do much of what Al's script did anyway 😕

        TIG

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdmitch
          last edited by 28 May 2015, 13:07

          @cesaro36 said:

          Hi Al
          I'm trying to use your rbeautify.rb but I get this error:

          RBeautify.beautify_file("c:/rotular.rb")
          Error: #<NoMethodError: undefined method each' for #<String:0x0000000fef1050>> C:/rbeautify.rb:90:in beautify_string'
          C:/rbeautify.rb:194:in beautify_file' <main>:in <main>'
          SketchUp:1:in `eval'

          where rotular.rb is just a generic rb that I'm using as a sample.
          Could you please help me?

          Tks

          I just downloaded this plugin and the line numbers,C:/rbeautify.rb:90:in beautify_string' and C:/rbeautify.rb:194:in beautify_file', don't seem to match the current version. My initial test produced a similar error but at C:/rbeautify.rb:146:in beautify_string' and C:/rbeautify.rb:368:in beautify_file'.

          After changing line 146 from "source.each do |line|" to "source.each_line do |line|", the plugin worked. So I would suggest downloading a new copy and make that change.

          Nothing is worthless, it can always be used as a bad example.

          http://sdmitch.blogspot.com/

          1 Reply Last reply Reply Quote 0
          • A Offline
            Al Hart
            last edited by 29 May 2015, 17:41

            [Seems to be working ok - give it a try]

            SketchUp 2014 and 2015, (ruby 1.9), changed the name of the .each function for strings.

            Change line 146 or 147 to read:

            source.each_line do |line|

            I have also uploaded a new version into the original post.

            Al Hart

            http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
            IRender nXt from Render Plus

            1 Reply Last reply Reply Quote 0
            • S Offline
              sdmitch
              last edited by 29 May 2015, 18:21

              Al, The only problem I have encountered is a line with multiple closing braces, ie }}}.

              This causes an imbalance between indents and outdents. I don't know enough about regular expressions to know it is even possible to trap such a string.

              Nothing is worthless, it can always be used as a bad example.

              http://sdmitch.blogspot.com/

              1 Reply Last reply Reply Quote 0
              • C Offline
                cesaro36
                last edited by 30 May 2015, 01:52

                Wow thank you all,
                Now it does what I needed.
                Anyhow I found a workaround by using the sublime text 2 code editor.
                Here it is:
                https://packagecontrol.io/packages/RubyFormat

                Gracias

                ¡Saludos a todos!

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Al Hart
                  last edited by 31 May 2015, 18:38

                  I found a different problem. I was using this code to set a string:
                  (This loads all text up to the word HERE into a string with new lines, etc.)

                  ` smess = <<HERE
                  This face will not render in the material you have created, since it is on placed the reverse side of a face.

                  Use "Setup Options/Special/Process 2 Sided Faces" to allow materials on reverse faces to be rendered.
                  HERE`

                  and the beautifier indented the last line (HERE) which cause the ruby to fail

                  Al Hart

                  http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
                  IRender nXt from Render Plus

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement