Where to start?
-
defined?(comics)
tests for existancecomics.is_a?(Array)
or
comics.instance_of?(Array)
or
comics.class==(Array)
tests the class of thecomics
object -
@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 aHOME
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
-
@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.
-
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/
-
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/3So 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 = [entry, entry2] => [#<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 { |entry| entry.time}.reverse [#<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 {|entry| entry.fulltext.match(/cadillac/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 {|entry| 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 { |entry| entry.mood} [;sick, ;confused] Success!
So want to ask what is the entry in pipelines? |entry|
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.
-
-
@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
-
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}
-
edges
holds the output of thefind_all
method, which is a newArray
of the items that were found.|e|
is the block parameter list, which is passed into the block by the Ruby keywordyield
.
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
-
@rumcajs said:
Well, helpful. I found one plugin in my folder, which is in global namespace.
> if (e.typename=="Edge") >
Not only is it in global namespace - it uses
typename
to identity the type of entity (which many plugin does because the API docs sets a very poor example.).typename
is very very slow - avoid at all cost! More details: http://www.thomthom.net/thoughts/2011/12/never-ever-use-typename/ -
@thomthom said:
Not only is it in global namespace - it uses
typename
to identity the type of entity (which many plugin does because the API docs sets a very poor example.).typename
is very very slow - avoid at all cost!Exactly. I am reading it just in the first link (http://forums.sketchucation.com/viewtopic.php?f=180&t=10142) in the first response in this thread.
-
Use
puts
statements in the code, to output to the Ruby Console. -
@rumcajs said:
I would like to see what contains
@iptemp
.It is an Sketchup::InputPoint instance object. See the API dictionary.
-
What debug tools and methods can I use to debug method?
Example, utilitiestool.rb
class TrackMouseTool
method onMouseMove@iptemp.pick view, x, y
I would like to see what contains @iptemp.
Is it possible to show dialog box or pause code?
-
If I change code of the plugin, will it be refreshed automatically or must I reload the plugin and how?
-
@rumcajs said:
If I change code of the plugin, will it be refreshed automatically or must I reload the plugin and how?
Reload it manually.
load 'myfile.rb'
Though I think Jim created a utility to autoreload after changes where detected. Never used it though as I prefer the manual control.
-
OK. Works. So I tried:
@iptemp.pick view, x, y puts @iptemp
#Sketchup::InputPoint:0xd0fe6fc
So the input point is in hexadecimal format? If I would want to read it, I would need to add method to convert to decimal, right? I tried:
puts @iptemp.hex
returns error -
No - the hex number you see there is the hexadecimal version of the object id. Ruby convention.
Refer to the API manual and you find
InputPoint.position
https://developers.google.com/sketchup/docs/ourdoc/inputpoint#position -
Ah, now it works.
puts @iptemp.position
Something I miss in the reference manual. From different languages I am used to see above every method a list of similar or related methods/links.... Is here some similar page which gives such kind of information / organized in different way? Or is this the only source?
-
What does mean $ is it global variable? I thought it is array, but now I see they use it in different way.
This:
results = inputbox prompts, values, $exStrings.GetString("Cost Estimate")
What is inputbox, does not look like method of global scope
Advertisement