by rvs1977 » Tue Jun 19, 2012 9:09 pm
hi! I have a problem passing array from Webdialg (Javascript) to Ruby. Htmlfile.html - Code: Select all
<script> var layer_array = [108,190,95]; query = 'skp:get_array@' + layer_array; window.location.href = query; </script>
Rubyfile.rb - Code: Select all
my_dialog.add_action_callback("get_array") do |web_dialog,yv_array| UI.messagebox "YV_array from callback action; " + yv_array end
In the html/js file its possible to do like this: alert("first item: " + layer_array[0]); => Output: "first item: 108" Or if I do like this: alert("all items: " + layer_array); => Output: "all items: 108,190,95" Every things work fine! But, when its send back to ruby the trouble starts: If I write: UI.messagebox ("first item: " + layer_array[0]) => Output: "first item: 57" But if I write: UI.messagebox ("all items: " + layer_array) => Output: "all items: 108,190,95" Here it seems to work??? My guess is when its send from JS to Ruby it goes from being an array to be a string? Anyone know how to overcome this problem? -Rasmus
--- Rudy
-
rvs1977
-
- Posts: 48
- Joined: Thu May 20, 2010 7:54 am
- Name: Rasmus V Sorensen
by Aerilius » Tue Jun 19, 2012 9:43 pm
The webdialog returns a string, for example "[1,2,3]". In Ruby, you always need to have a clear idea what type your value has. In JavaScript, you can mix incompatible types hoping that it will convert it and work as expected, but not in Ruby. Don't miss to read the Ruby Newbies Getting Started GuideIf you apply an index to a string, you get the character code of the character at that index, example "string"[0] = 115 # corresponds to 's'When you later added a string + the arraystring, you got the expected result because both were strings. You can't add an array to a string, because it would give: Error: #<TypeError: (eval):151:in `+': can't convert Array into String>The + method is not defined for different types. It exists separately for Numeric, for Strings and for Arrays, and if you have different types, you have to convert them into the same so that you can use one of the methods for it (either with array.to_s or array.inspect). You could either parse the string into an array for example - Code: Select all
@yv_width_array_rs = yv_width_rs[1..-2].split(",")
which returns an array of strings. If it should be floats (or integers) then you loop over the resulting array with collect and convert each string of a float into a float with to_f: - Code: Select all
@yv_width_array_rs = yv_width_rs.scan(/\d+/).collect{|d| d.to_f} # or d.to_i
Or you eval the array - Code: Select all
begin @yv_width_array_rs = eval(yv_width_rs) rescue @yv_width_array_rs = [] end
which you should absolutely avoid if there is an alternative way.
Last edited by Aerilius on Tue Jun 19, 2012 10:07 pm, edited 1 time in total.
-
Aerilius
- Top SketchUcator
-
- Posts: 1181
- Joined: Tue Dec 23, 2008 11:00 pm
- Location: Kaiserslautern
- Name: Aerilius
by Dan Rathbun » Tue Jun 19, 2012 9:44 pm
First of all... using UI.messagebox as a debugging tool is a bad idea. They often fail silently, if the message argument is not correct, or there are Type mismatches in the expression. Get in the habit on opening the Ruby Console, and using puts() to output debug strings. And you can use the .to_s() or .inspect() methods to be sure the output is String compatible: - Code: Select all
my_dialog.add_action_callback("get_array") do |web_dialog,yv_array| puts "YV_array from callback action; " + yv_array.inspect end
Also the double-quoted String parameter replacement notation, #{expression}, has built-in text conversion for many Ruby base classes. So this may also work: - Code: Select all
my_dialog.add_action_callback("get_array") do |web_dialog,yv_array| puts "YV_array from callback action: #{yv_array}" end
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4146
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Dan Rathbun » Tue Jun 19, 2012 9:52 pm
Aerilius wrote:If I am right the webdialog returns a string, for example "[1,2,3]" ... Or you eval the array ...
Yup you are correct, and eval() is the simplest way.. provided that on the Js side, you build the strings so Ruby's eval() can read them. The API Dictionary at WebDialog#add_action_callback wrote:Your JavaScript in the webdialog will invoke the callback method with a string representing arguments to the callback method.
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4146
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Dan Rathbun » Tue Jun 19, 2012 9:58 pm
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4146
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by thomthom » Tue Jun 19, 2012 10:19 pm
When you experience trouble with Ruby <-> JavaScript check the compiled string you send backwards and forwards. You will when quickly see syntax errors.
-

thomthom
- Global Moderator
-
- Posts: 17925
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: architecture
- Level of SketchUp: Advanced
-
by rvs1977 » Wed Jun 20, 2012 5:53 am
Thank you all!! Now I have something to try out. 
--- Rudy
-
rvs1977
-
- Posts: 48
- Joined: Thu May 20, 2010 7:54 am
- Name: Rasmus V Sorensen
by Dan Rathbun » Wed Jun 20, 2012 6:30 am
Aerilius wrote:Or you eval the array - Code: Select all
begin @yv_width_array_rs = eval(yv_width_rs) rescue @yv_width_array_rs = [] end
Here's a good place to show, how to use the rescue keyword, in modifier position (and save 4 lines of code.) @yv_width_array_rs = eval(yv_width_rs) rescue []
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4146
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by rvs1977 » Fri Jun 22, 2012 2:30 pm
Aerilius, Im a little impressed. When you quote my code, you use different variable-names than the ones from my example. The funny thing is, the variable-names you use is the one I actually use on my own computer, before I started to look at arrays... Are you psychic or something?? 
--- Rudy
-
rvs1977
-
- Posts: 48
- Joined: Thu May 20, 2010 7:54 am
- Name: Rasmus V Sorensen
by Aerilius » Fri Jun 22, 2012 5:55 pm
I'm not psychic but rather impressed/surprised myself. That are the variable names that you posted  and after submitting my response, you had modified/improved the original question that I didn't recognize it anymore 
-
Aerilius
- Top SketchUcator
-
- Posts: 1181
- Joined: Tue Dec 23, 2008 11:00 pm
- Location: Kaiserslautern
- Name: Aerilius
by rvs1977 » Fri Jun 22, 2012 9:57 pm
ah ok Anyway, it finally works with this "simple" solution: - Code: Select all
my_dialog.add_action_callback("get_array") do |web_dialog,yv_array| @YV_array = yv_array.scan(/\d+/).collect {|d| d.to_i} end
I wonder why the eval-rescue solution has to be avoided? Is it slow? Thanks to all of you. - Aerilius, Dan, Thomthom ---------------- Gratias Ad Omnes
--- Rudy
-
rvs1977
-
- Posts: 48
- Joined: Thu May 20, 2010 7:54 am
- Name: Rasmus V Sorensen
by Dan Rathbun » Fri Jun 22, 2012 10:34 pm
rvs1977 wrote:I wonder why the eval-rescue solution has to be avoided? Is it slow?
ALL String operations in Ruby 1.8.x are slow. eval() is the Ruby code parser.. so it is a very large method (actually a C-side function.) But I cannot see it being slower than the 2 iterator methods you are using, in addition to the type converter. But it works... and you understand how it works. If speed is not an issue, then go with it ...  Also if you KNEW each numeric was separated by a "," then you also do: @YV_array = yv_array.split(',').map{|i| i.to_i}
-

Dan Rathbun
- Top SketchUcator
-
- Posts: 4146
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2013
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
Return to Developers' Forum
|