Interesting AI generated Ruby Code example

I have been playing around with various AI programs to see what they can do. I though why not see if it can generate any code.

This is what I asked:

write ruby code to draw a 10’ x 10’ box in SketchUp. I did not specify any particular version of SketchUp. This is what it returned:

Define the dimensions of the box

width = 10.feet
height = 10.feet
depth = 0

Create a new group to contain the box

group = Sketchup.active_model.entities.add_group

Create the points for the corners of the box

points =
points[0] = [0, 0, depth]
points[1] = [width, 0, depth]
points[2] = [width, height, depth]
points[3] = [0, height, depth]

Create the face for the bottom of the box

face_bottom = group.entities.add_face(points)
face_bottom.reverse!

Extrude the face to create the box

face_bottom.pushpull(-depth)

Create the faces for the sides of the box

group.entities.add_face(points[0], points[1], points[2], points[3])

Refresh the view to display the box

Sketchup.active_model.active_view.refresh
Make sure you have SketchUp installed on your system and the Ruby console activated within SketchUp. Copy and paste the code into the Ruby console, and it will create a 10’ x 10’ box in the SketchUp modeling environment.

Create a new group to contain the box

group = Sketchup.active_model.entities.add_group

Create the points for the corners of the box

points =
points[0] = [0, 0, depth]
points[1] = [width, 0, depth]
points[2] = [width, height, depth]
points[3] = [0, height, depth]

Create the face for the bottom of the box

face_bottom = group.entities.add_face(points)
face_bottom.reverse!

Extrude the face to create the box

face_bottom.pushpull(-depth)

Create the faces for the sides of the box

group.entities.add_face(points[0], points[1], points[2], points[3])

Refresh the view to display the box

Sketchup.active_model.active_view.refresh

#Make sure you have SketchUp installed on your system and the Ruby console activated within SketchUp. Copy and paste the code into the Ruby console, and it will create a 10’ x 10’ box in the SketchUp modeling environment.

I ran it and it first said undefined variable depth

After reinstalling Ruby I got this error:

Code failed with an error: NameError: undefined local variable or method `response’ for main:Object

What is the AI program missing? How do I defined a local variable or method?

I don’t code myself in Ruby code, but I thought what AI was able to generate was sort of fascinating. Maby a little scary too.

[How to] Post correctly formatted and colorized code on the forum?

A lot. The AI thinks anything posted on the web is gospel truth, including now it’s erroneous code that you’ve just pasted onto the web.

Apparently, the AI did not really study Ruby or SketchUp’s API very well.

We’ve already discussed the AI’s limitations in a couple of recent topic threads. (Search this category.)

Please read a primer on Ruby coding. See the learning resource list I’ve compiled here:

2 Likes

Dan,
Thanks. I don’t frequent this forum too much unless I’m notified.
I have some experience (rusty) with Autolisp. but none with Ruby. As you know, there are many useful extensions for Sketchup that have been written using the Ruby API that I know nothing about.
I did find it rather fascinating the AI was able to attempt what it did, even if it didn’t follow proper rules you would use. It is after all machine learning.

Who programmed it for Ruby? Must have been a terrible programmer if the code was as bad as your review Will it get better? I don’t know. Will it become smarter than humans? My guess is, for certain things yes. But it will always need humans to make it work exactly as intended. I would not be afraid of it, at least not yet.

So, how would this code be fix to do what I told it to do? Can you provide an example that I can compare? Understand that I am not a programmer, but I can read and understand the difference between right and wrong.

AutoLISP & Lisp ("stupid parenthesis land’) are a functional language (ie, every expression is a function call,) whereas Ruby is a more modern multi-paradigm dynamic object-oriented language. Really, take a few minutes to read some of the primers linked to in my resource list. (Warning, … you might become addicted to Ruby!)

Again, please correctly delimit the code block in the 1st post so we can at least read it as code.

Basically, the AI (I think) does not read the SketchUp application release notes nor the API release notes. This is indicated by it’s not putting a condition upon reversing the initial face created on the ground plane. In the recent 2021.1 release, SketchUp was changed to stop making faces on the ground plane facing down. Since, it now creates “butter-side up” faces, the face_bottom.reverse! statement should be conditional, ie:

face_bottom.reverse! unless face.normal.samedirection?(Z_AXIS)

Another glaring error is that the pushpull operation has already created the other sides of the box. So, the following comment and “side” face generation statement is erroneous.

Thirdly, the view.refresh method is strongly discouraged by the API documentation and also is not necessary to display the changes.

The most glaring error is that the AI did not wrap the code within an undo operation.


The code (as posted) looks weird. It looks like part of it was posted twice.
The points array and the group need only be created and push-pulled once

I will take some time to try to understand Ruby a little better. I won’t make be an overnight programmer but I might be better able to understand your technical explanation. I’m an old guy now, pretty much retired from forty years as an architect. It was not always easy, but I found that people who are not trained in a particular science or profession just don’t understand even what we think is non-technical language. I thank you for your replies but so far, it’s way over my head. I do however understand some object oriented language as opposed to interpreted language Autolisp Easy vs hard. but served a valid purpose.

Can you please ask AI to wrap you first post into the format as Dan mentioned in his first sentence of his first answer!

I also asked again in my second answer. :roll_eyes:

But the AI did not and does not post any code here. (And the AI would not know that the code was intended to be posted in a forum. It was only after it failed that the code was posted.)

He can also ask IA for corrections…

DanRathbun
I may be wrong but I don’t feel that Ruby is nearly as well documented as most other programming languages. It seems to be okay but not great to meet the needs of Sketchup. It is though just another interpreted language, not unlike Autolisp so I don’t quite understand what I perceive to be animosity for Autolisp. I have played a little with C, C++, Python and of course HTML for web design. Ruby’s syntax is not exactly logical but really not that much different than the basics of most programming languages.
It also appears that Ruby is version specific. I have been trying to follow a Chris Fullmer tutorial. The version is unknown. I’m using Sketchup Pro 2021 and I think his tutorial is from around 2012. Some of the most basic code gives me syntax errors whether it type the code into a ruby editor or copy and paste it.
I think your advise to start with some of the Wiki files is good advise but I would like to find a more concise book with tested code that works.

I have not tried to tell AI to edit anything that it has generated. I am not sure if it is capable of doing so. It might be interesting experiment to try though. As far as delimiting and beautifying code, I think that is mainly a function if the editor one uses. If I use Sketchup’s standard ruby console, it is bare bones and no better than notepad. If I use Dreamweaver or another 3rd party editor, the output is usually better formatted.
When I am sending AI code to this forum, it is from cutting and pasting from one editor to another. It generally loses any formatting it may have had. Sketchup’s forum does have some basic editing features that can make replies look okay but not all of us are as particular about indenting and colorizing code as others.

Yes, Ruby has branches and versions.

That was circa SketchUp version 8 MR 3…5, just after Trimble bought SketchUp from Google.
In those days and in the following 2013 release, SketchUp came with Ruby version 1.8.6-p287.

Beginning with the 2014 release, SketchUp came with it’s own cross-platform Ruby version 2.0.0 and the Ruby Standard Library. (The older releases using 1.8.x Ruby did not come with the Standard Library.)

Every 2 to 3 years (or so) SketchUp has updated the Ruby version it ships with.

SketchUp 2021.0 shipped with Ruby version 2.7.1. In the 2021.1 release Ruby was updated to version 2.7.2.

At the Ruby Console you can ask Ruby what version it is by typing this constant:

RUBY_VERSION

You can find the Ruby Core Docs here:
Index of Files, Classes & Methods in Ruby 2.7.2 (Ruby 2.7.2) (ruby-doc.org)
… and the Standard Library here:
Ruby 2.7.2 Standard Library Documentation (ruby-doc.org)

I think he was being sarcastic because I had asked twice for you to correct the code posting in your 1st post.

Okay, for the last time … READ THIS:

Then, go back up to the 1st post, and click the pencil icon to edit the post, and insert the delimiters for posting code in this forum, before and after the block of code.

I will no longer help you with code unless it is properly posted.

Good luck with that. I again refer you to the book lists in the resources I posted a link to above.

I will point you toward one of the links in my Ruby Learning Resources list …

It helps coders familiar with certain languages understand the differences and similarities of Ruby versus these other programming languages.

Other than this, debating what coding language is best, better or worse is just a general waste time.
SketchUp has Ruby and C APIs. Take your pick.

2 Likes