Quotation marks in Html - Id(variable) - working in Sketchup Ruby Code Editor

We are working in Sketchup, using its Ruby Code Editor and we are programming a UI::WebDialog using Html and JavaScript. We want to define document.getElementById(variable).innerHTML using a variable. How would you add the single quotation marks?

(Please post your Ruby questions in the Ruby API subcategory.)


Is the variable a Ruby or JavaScript reference ?

What single quotation marks ?

… ie, we cannot read minds. :roll_eyes:


If Ruby, then you must use a double-quoted string in order to do interpolation.

I explained this in the other topic thread.

# Ruby

dlg.execute_script(
  %[document.getElementById("#{ruby_id}").innerHTML="#{ruby_value}";]
)

I again point you to the Ruby Core primer on String literals …

https://ruby-doc.org/core-2.5.5/doc/syntax/literals_rdoc.html#label-Strings


If you are doing this within JavaScript, … (which I doubt because you said your are using a SketchUp Editor extension,) … you just use a string reference that holds string ID.

Unless you are targeting versions older than 2017 I would recomend switching to the newer UI:HtmlDialog.

1 Like

and when using UI::HtmlDialog it displays in a window using the Chrome Embedded Framework which allows using the Chrome DevTools as I showed in the other topic, post 12

When testing your HTML dialog, just right-click on your dialog window, and choose “Show DevTools” …

image

I’m still working in Ruby Code Editor, and the variable is a JavaScript reference. I’ve tried some published solutions for JavaScript but they don’t work here.

That isn’t good for developing an extension. It’s only useful for playing around (learning) with one liners or small snippets that do concise tasks.

As said, it has been known to introduce problems. It itself is a html dialog, that sends commands and script text to Ruby, which evaluates it, and would then send command to your html dialog.

Well if the JavaScript reference is a String, and it exists in the dialog’s JS, and it matches an ID given to one of the documents elements, … there should not be an issue. You do not used quotes around an object reference in any coding language.

So … from Ruby …

dlg.execute_script(
  %[document.getElementById(js_var}).innerHTML="#{ruby_value}";]
)

There IS a quirk with JS’s innerHTML= and Ruby strings. There cannot be any newline characters at the end of any of the HTML text in the Ruby string. If there are, the JS innerHTML= method can fail (perhaps silently.)

Since you do not have control over the Ruby strings sent to your dialog, (ie the “Ruby Code Editor” has all the control,) you will not be able to overcome that barrier, if it occurs.


In my extensions, when I load the HTML from a file, (and I have this problem) … I use a :chomp option to remove newline characters. IE … (a snippet from my code) …

      # Load the HTML for each listing line striping out newline characters.
      # We must do this because the JavaScript method call element.innerHTML=
      # within CEF fails if the HTML text contains embedded Ruby newlines:
      list_line = File.readlines(filepath, chomp: true).join('')

If you insist working in this way you’ll continue to run into problems. I don’t have any more time to deal with silly issues.

My last advice to you is to ditch that “Code Editor”, and get a real editor application like:

Then code like a real programmer codes in files, and load your files as an extension loads when SketchUp starts.

1 Like

As in any language, the natural learning process begins by repeating the words of others to try to say what you want to say; wich is particulary difficult when it comes to programming in Ruby+JS. I borrowed a WebDialog old example and I have managed to display my arrays in my html window. At the moment I don’t have enough level to assimilate all the information from your posts, although they are there for when they can help in this learning process.

As I said, I’m a novice Ruby person who started to program in Ruby, using the Skup Oficial Code Editor, 2 months ago, with the intention of programing, basically working on object attributes and exchanging information with excel, where I usually work. I didn’t expect the problems with the Skup 'Oficial’Code Editor or the recomenation to use JS and Html to display information in a proper way, which implies added problems, mostly unsolved from years ago. This silly issues don’t occur when you have worked properly to offer people a way to program. So they are the unexpected Sketchup Developers silly issues that any novice has to deal with. I will be very grateful to get all these unexpected information in advance, or your understanding when I keep saying or doing things in a wrong way, just because I didn’t get that information. Following your recomendation, I sincerely don’t know how I should program from other editors and run the program over my model. Thanks in advance. I will understand if you leave the forum. They are opened questions.

Here are the steps. I recommend VS Code.

Also (contrary to Dan’s advice), I find Ruby Console + to be quite useful. You still need to use the built in ruby console to get accurate error info. That is what Dan mentioned. I find Ruby Console +, to be really handy when doing quick testing. You can use the built in tool to easily get a reference to an object in the model.

https://extensions.sketchup.com/extension/b3865233-5e84-4f7e-8342-517aca889225/ruby-console

1. The is no such word as “Skup”.

2. There is no “Official” SketchUp Code Editor.

Instead, there are several 3rd party extensions, that are implemented in a web html dialog.

Unfortunately, the reality is that it is not Trimble’s responsibility to teach any kind of programming. Only to document their APIs.

I personally have spent many days and hours to compile lists of resources for novice SketchUp Ruby coders …

The list has links to free downloadable books on Ruby programming, as well as links to online blog posts and tutorials, many are SketchUp specific.

There are also a plethora of tutorials on the web for HTML, CSS, and JavaScript.
A forum is not really an adequate place to teach programming.

I am sorry that there is no way that we can “pour” the coding knowledge into novice brains.

Any person’s ability to absorb and apply new coding knowledge will depend upon their current foundation of the person’s programming knowledge.

This is why Steve asked you right away in the other topic on web dialogs …

… but you never really gave a good answer. So we didn’t have any good idea of your ability level.

So this basically makes me feel as though I’ve wasted two days of my time, when it comes to specifically helping you.

It is no one else’s responsibility but your own, to seek out and learn basic programming knowledge, so that when you ask questions, you are able to understand the answers.

I am not leaving. I am just not going to waste any more time giving advice that will not be used.

Hopefully other readers may find it useful.

Sorry Dan,

**1. There is no such word as “Skup”
I used Skup as an abbreviation of Sketchup.
**2. There is no “ Official ” SketchUp Code Editor.
You are right, they are no “Official” SketchUp Code editors, but they are recommended extensions that do what they should do, considering what you can learn from https://ruby.sketchup.com/ I thought they were the better way to start programming with Ruby. I must confess that I’m lost when I read Ruby’s articles outside of the SketchUp environment, or I find any sample Ruby code. I have verified that there are commands not working in any of the Sketchup Code Editors, without any reference in https://ruby.sketchup.com/, so I avoid those commands and those websites. Nevertheless, I think it’s better to use extensions to run the code easily and facilitate my learning process.

I think it is offensive to use the adjective “silly” to refer to someone’s issue. That will keep beginners away from this forum. Nor are they such silly problems when they need such complex solutions. Therefore, it is understandable that these issues, not well resolved, appear repeatedly for many years in this forum. So, if something has to be silly, it should be that nobody notices it or nobody improves it.

Trimble’s responsibility? Offer easy ways to do easy things. I appreciate the capability to create dictionaries with your own attributes, create lists, … but it’s dificult to understand why you can’t show or work with all this information in an appropriate way. You shouldn’t need to leave ruby to do all these kinds of things. By doing this, you are exposing your code to errors, incompatibilities with browsers, … Thus, I have to settle for using the dynamic attribute dialog box, with no possibility to group my information, nor use drop-down windows. And, this is not an attack, this is an attempt to make a constructive contribution. I love Sketchup too.

I very much appreciate your Ruby Learning Resources and you can be certain I will read them as soon as I can. I don’t understand why you believe that you are wasting your time. I’d never worked with HTML, CSS, or JavaScript, and I’m a beginner with Ruby. Your posts have been at a very high level and they are not easy to follow when you haven’t achieved that level. So, if this is not really an adequate place to teach programming with such languages, it is advisable to postpone them until my Ruby’s learning process will finish.

I hope that you will not be offended, it was never my intention.

1 Like