sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Where to start?

    Scheduled Pinned Locked Moved Developers' Forum
    44 Posts 4 Posters 780 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.
    • R Offline
      rumcajs
      last edited by

      @thomthom said:

      Have a look at the Ruby in Twenty minutes link. It goes through all the basics like this.

      Yeah, I've just finished.

      Is it only way of detecting if variable is array or do you have more ways? (inside class)

      
      if @names.respond_to?("each")
      end
      
      1 Reply Last reply Reply Quote 0
      • R Offline
        rumcajs
        last edited by

        I'm having a fun with this:
        http://tryruby.org/levels/1/challenges/0#levels/1/challenges

        This interactive guide for ruby is really cool. Somebody could make one for ruby beginners in SU, don't you think? It looks simple to do something like that.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          @rumcajs said:

          @thomthom said:

          Have a look at the Ruby in Twenty minutes link. It goes through all the basics like this.

          Yeah, I've just finished.

          Is it only way of detecting if variable is array or do you have more ways? (inside class)

          
          > if @names.respond_to?("each")
          > end
          

          @names.is_a?( Enumerable )

          Btw, - strings are slow, avoid them if you can. For respond_to?("each") use Symbols instead - like so: respond_to?(:each)

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • R Offline
            rumcajs
            last edited by

            @thomthom said:

            Btw, - strings are slow, avoid them if you can. For respond_to?("each") use Symbols instead - like so: respond_to?(:each)

            Yes, I read it just now in the interactive guide 😉 I create my first hash. But don't know what is it hash, looks like an object... Why they call it hash?

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              I take it you come from Javascript background?

              Object have methods - Hashes has data.
              A hash is an associative array - you specify the key and value. While with an array you just insert or remove values. (PHP and Javascript blends the difference between Hashes and Arrays. In Ruby they are distinct. ) Note that the order of the elements in arrays is based on the order they where added. When you iterate a Hash the values and keys is returned in a different order from how they where inserted.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • R Offline
                rumcajs
                last edited by

                OK. So summary.

                Objects can keep properties (@name...) and methods.
                Arrays [] are only non-associative.
                Hashes {} are associative arrays, not ordered.

                Can you print function or method just like in JS? I know more scripting languages not just JS. Last one is AHK.

                1 Reply Last reply Reply Quote 0
                • R Offline
                  rumcajs
                  last edited by

                  I tried this (interactive guide):

                  ratings = Hash.­new(0)
                  => {}
                  Success!
                  > books.valu­es.each { |rate­| ratin­gs[rate] += 1 }
                  => [;splendid, ;quite_good, ;mediocre, ;quite_not_good, ;abysmal]
                  > ratings
                  => {;splendid=>1, ;quite_good=>1, ;mediocre=>1, ;quite_not_good=>1, ;abysmal=>1}
                  >  
                  

                  And it is not clear to me, hoq the value 1 is assigned to the values. I guess that first command created new hash ratings. Then I go through each value of the hash and set the ratings... But how is it possible, that ratings hash overwrite the book.values? As I know this is problem in PHP. If you know something like

                  foreach(books->valu­es as $val){ 
                  ; this will not work;
                  val="new value";
                  }
                  

                  It will not overwrite the for values of "values" property. Maybe the relation between ratings and books.valu­es was already defined somewhere in a class, which is not visible to me?

                  This command does not work to me as expected:

                  .. File.Open(­"/Home/com­ics.txt", "a") do |f|
                  .... jjj
                  .. ss
                  

                  I can write in the file but how to close it and to keep data in it? There should be some other stuff in the file when I write File.Open(­"/Home/com­ics.txt", "a") . Once I succeeded there were some links and they say, that is should add some data in file...

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @rumcajs said:

                    I tried this (interactive guide):

                    ratings = Hash.­new(0)
                    > => {}
                    > Success!
                    > > books.valu­es.each { |rate­| ratin­gs[rate] += 1 }
                    > => [;splendid, ;quite_good, ;mediocre, ;quite_not_good, ;abysmal]
                    > > ratings
                    > => {;splendid=>1, ;quite_good=>1, ;mediocre=>1, ;quite_not_good=>1, ;abysmal=>1}
                    > >  
                    

                    And it is not clear to me, how the value 1 is assigned to the values.

                    Because the Ruby interpreter changes the shorthand a += b, to a = a + b

                    So your statement:
                    books.valu­es.each { |rate­| ratin­gs[rate] += 1 }

                    is actually executed as:
                    books.valu­es.each { |rate­| ratin­gs[rate]= ratin­gs[rate] + 1 }

                    😄

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      Method names begin with a lower case character (by convention.)

                      So it is:

                      ` File.open("path/to/file","w") do |f|

                      end`

                      The block form of IO::open with automatically close the file handle.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • R Offline
                        rumcajs
                        last edited by

                        Something else confused me. But i think I had overlooked something. The ratings are not saved in values, but in ratings...

                        Things are going bad, I cannot move on.

                        
                        #<SyntaxError; Invalid char "\xC2" in expression. near line 1; "\xADread(\"/Hom\xC2\xADe/comics.t\xC2\xADxt\")">
                        > print File.­­read("/co­mics.t­xt"­)
                        =>
                        #<SyntaxError; Invalid char "\xC2" in expression. near line 1; "\xADread(\"/comics.t\xC2\xADxt\")">
                        > print File.­read("comi­cs.txt")
                        =>
                        #<Errno;;ENOENT; No such file or directory - comics.txt>
                        > Dir["/*"]
                        => []
                        Open up a new BlogEntry class, pretty pretty please.
                        >  
                        
                        

                        How can I print what is in /home/comics.txt and /comics.txt and if they exists and why this error happens...

                        1 Reply Last reply Reply Quote 0
                        • R Offline
                          rumcajs
                          last edited by

                          Whatever i write it jumps into input mode:

                          comics.is_­a
                          .. end
                          > comics.is_­a?
                          ..  
                          
                          

                          Damn, that's not 15 minutes tutorial, but 15 hours (I'm in half but cannot move on).

                          
                          > Popup.make­ do
                          .... h1 "Header"
                          .... list do
                          ...... comics.eac­h do |name­, url|
                          ........ link name,­ url
                          ........ end
                          ...... end
                          .... end
                          ..  
                          
                          

                          Should generate web page, but nothing popups. And I cannot check if the array exists, what is in array, what is in directory or if the file exist... Look like not realy ruby console (running in browser). 👎

                          1 Reply Last reply Reply Quote 0
                          • Dan RathbunD Offline
                            Dan Rathbun
                            last edited by

                            defined?(comics)
                            tests for existance

                            comics.is_a?(Array)
                            or
                            comics.instance_of?(Array)
                            or
                            comics.class==(Array)
                            tests the class of the comics object

                            I'm not here much anymore.

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              @rumcajs said:

                              How can I print what is in /home/comics.txt and /comics.txt and if they exists and why this error happens...

                              You cannot assume that the current working directory is what you wish it to be (because another plugin may have changed it.)

                              Be sure a HOME environment var is defined on PC (it is always defined on Unix-like systems, such as OSX.)

                              unless RUBY_PLATFORM =~ /(darwin)/i
                                ENV["HOME"]= ENV["USERPROFILE"] unless ENV["HOME"]
                              end
                              
                              
                              
                              oldDir = Dir.getwd()
                              Dir.chdir(ENV["HOME"])
                              
                              if File.exist?('comics.txt')
                                lines = IO.readlines('comics.txt')
                                puts(lines)
                              else
                                puts("'comics.txt' could not be found.")
                              end
                              
                              Dir.chdir(oldDir)
                              
                              

                              or use the **~** shortcut (if you KNOW that a HOME environment var is defined.)

                              filepath = File.expand_path('~/comics.txt')
                              if File.exist?(filepath)
                                lines = IO.readlines(filepath)
                                puts(lines)
                              else
                                puts("'#{File.basename(filepath)}' could not be found in dir;\n#{File.dirname(filepath)}.")
                              end
                              
                              

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                @rumcajs said:

                                Things are going bad, I cannot move on.

                                Because you NEED to read "Programming Ruby: The Pragmatic Programmer's Guide"

                                You need to take a WEEK OR MORE getting comfortable with Standard Ruby, before attempting to learn to use the SketchUp API.

                                I wrote the "Ruby Newbie's Guide to Getting Started" to help you and others avoid the frustration you are experiencing.

                                Follow the guide and you will be much happier.

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • thomthomT Offline
                                  thomthom
                                  last edited by

                                  When it comes to the Ruby API I have a few articles on my blog - don't think they are liked in the sticky threads.

                                  I recommend new plugin developers to read this one first: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • R Offline
                                    rumcajs
                                    last edited by

                                    I run this "Ruby" in Browser, this is not in SU. It is tutorial - interactive online guide which is called "15 minutes tutorial"
                                    http://tryruby.org/levels/1/challenges/0#levels/8/challenges/3

                                    So I am not sure if the ruby runs there or it is just simulation of Ruby in Javascript or Flash. So as they say, the path is preconfigurated, and I checked before that the path and files were there. Now I am on next lesson.

                                    
                                     blog = [entr­y, entry­2]
                                    => [#<BlogEntry;0x72e8e4f7 @fulltext="I can't believe Mt. Hood was stolen! I am speechless! It was stolen by a giraffe who drove away in his Cadillac Seville very nonchalant!!", @time=2012-09-16 15;55;25 +0000, @title="Today Mt. Hood Was Stolen!", @mood=;sick>, #<BlogEntry;0x18104f11 @fulltext="I am
                                      never going back to that mountain and I hope a giraffe steals it.", @time=2012-09-16 15;55;25 +0000, @title="I Left my Hoodie on the Mountain!", @mood=;confused>]
                                    Success!
                                    > next
                                    > blog.sort_­by { |entr­y| entry­.time}.rev­erse
                                    [#<BlogEntry;0x263b3097 @title="I Left my Hoodie on the Mountain!", @time=2012-09-16 15;57;13 +0000, @fulltext="I am\
                                      never going back to that mountain and I hope a giraffe steals it.", @mood=;confused>, #<BlogEntry;0x77f31d1c @title="Today Mt. Hood Was Stolen!", @time=2012-09-16 15;57;13 +0000, @fulltext="I can't believe Mt. Hood was stolen! I am speechless! It was stolen by a giraffe who drove away in his Cadillac Seville very nonchalant!!", @mood=;sick>]
                                    Success!
                                    > back
                                    > blog.find_­all {|ent­ry| entry­.fulltext.­match(/cad­illac/i)}
                                    [#<BlogEntry;0x68f04725 @title="Today Mt. Hood Was Stolen!", @time=2012-09-16 15;58;56 +0000, @fulltext="I can't believe Mt. Hood was stolen! I am speechless! It was stolen by a giraffe who drove away in his Cadillac Seville very nonchalant!!", @mood=;sick>]
                                    Success!
                                    > blog.find_­all {|ent­ry| entry­.fulltext.­match(/Mt.­{2,10}/i)}­
                                    => [#<BlogEntry;0x5ac78f1f @fulltext="I can't believe Mt. Hood was stolen! I am speechless! It was stolen by a giraffe who drove away in his Cadillac Seville very nonchalant!!", @time=2012-09-16 15;59;49 +0000, @title="Today Mt. Hood Was Stolen!", @mood=;sick>]
                                    Success!
                                    > back
                                    > blog.map { |entr­y| entry­.mood}
                                    [;sick, ;confused]
                                    Success!
                                    

                                    So want to ask what is the entry in pipelines? |entr­y|

                                    I defined entry and entry2 as instance of my little blog class. Blog is array containning entry and entry2, but I ask about what is meanning for |..| in these methods. It looks like it is name of class.

                                    1 Reply Last reply Reply Quote 0
                                    • R Offline
                                      rumcajs
                                      last edited by

                                      @dan rathbun said:

                                      @rumcajs said:

                                      Follow the guide and you will be much happier.

                                      The frustration is from the Interactive guide, because it didn't work in the fully meaning of ruby console. SO I could not check what I did. But I closed the window, and moved.

                                      1 Reply Last reply Reply Quote 0
                                      • R Offline
                                        rumcajs
                                        last edited by

                                        @thomthom said:

                                        I recommend new plugin developers to read this one first: http://www.thomthom.net/thoughts/2012/01/golden-rules-of-sketchup-plugin-development/

                                        Well, helpful. I found one plugin in my folder, which is in global namespace.

                                        
                                        #this part is taken (after a little modification) directly from the script Skin by Darrel Belvin so all credit is his (hope he doesn't mind me posting this)
                                        def clean_up
                                        	Sketchup.active_model.start_operation "Clean-up"
                                        	ss = Sketchup.active_model.selection.collect
                                        	erasee = []
                                        	ss.each {|e|
                                        		if (e.typename=="Edge")
                                        			if((e.faces.length==0)|| (e.faces.length==2 && e.faces[0].material==e.faces[1].material && e.faces[0].normal.parallel?(e.faces[1].normal)))
                                        				erasee.push e
                                        			end
                                        		end
                                        	}
                                        	erasee.each {|e|
                                        		e.erase! if(e.deleted? == false)
                                        	}
                                        	Sketchup.active_model.commit_operation
                                        	return
                                        end
                                        if not (file_loaded? "cleanup.rb")
                                        	clean = UI.menu("Plugins")
                                        	clean.add_item("Clean-up") {clean_up}
                                        end
                                        
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • R Offline
                                          rumcajs
                                          last edited by

                                          I go through SU examples.

                                          So Utilities - Create Faces ... utilitiestools.rb

                                          
                                          ss = Sketchup.active_model.selection
                                             
                                          # Get an Array of all of the selected Edges
                                          edges = ss.find_all { |e| e.kind_of?(Sketchup;;Edge) }
                                          
                                          

                                          So here the "e" means output of the find_all method, so what the loop produces in the moment of its cycle. I thought that it is definition of the input or some argument for input! But recalled that I asked on it on previous page. It seems to me that it is like loop, similar like .each { |e| e.some.action.here}

                                          1 Reply Last reply Reply Quote 0
                                          • Dan RathbunD Offline
                                            Dan Rathbun
                                            last edited by

                                            edges holds the output of the find_all method, which is a new Array of the items that were found.

                                            |e| is the block parameter list, which is passed into the block by the Ruby keyword yield.
                                            The number of block parameters can vary from one block method to another.

                                            This is an example of what the find_all method would look like, if it was written in Ruby (but it is actually written in C.)

                                            class MyArray
                                            
                                              def find_all
                                                raise(ArgumentError,"no block given!",caller) unless block_given?
                                                found = []
                                                self.each {|item|
                                                  # call the block using yield, passing in item
                                                  # if the block returns true via yield,
                                                  # push the item into the found array.
                                                  found << item if yield(item)
                                                }
                                                return found
                                              end
                                            
                                            end
                                            

                                            I'm not here much anymore.

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

                                            Advertisement