Ruby errors

Having problems with Ruby errors "#<SyntaxError: :215: syntax error, unexpected keyword_end, expecting ‘)’ " on every line I have and “end” onIcosa a 12 30 18 Create triangles .rb (7.0 KB)

Rubocop said you have 4 unnecessary end

Line #216 – Error: unexpected token kEND (Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
		end
Line #279 – Error: unexpected token kEND (Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
      end
Line #283 – Error: unexpected token kEND (Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
      end
Line #293 – Error: unexpected token kEND (Using Ruby 2.1 parser; configure using TargetRubyVersion parameter, under AllCops)
    end

you also redefine :radians and :degrees which is not required or sensible…

john

If you use a code editor with a Ruby mode its highlighting and indents will both help you find many syntax errors and avoid them in the first place. One of the first things I do when I open someone else’s code file is select all and tell the editor to re-indent it. Messed up block ends will cause obviywromg indentation.

The main cause is your commenting method call arguments at line 200 but not commenting out the start parenthesis.

      try[0]  =  (#{pnt[a,b]})
      try[1]  =  (#{pnt[a,b]})
      try[2]  =  (#{pnt[a,b]})

… so the interpreter is looking for the closing parenthesis, hence the phrase from the error messages …
"..., expecting ‘)’ " .


Your code needs to be wrapped within a author/company namespace module, and your Procs should just be plain methods.


Do not override globally any API or Ruby Core modules, classes or methods.


This makes no sense.

  angl_bm = Array.new
  angl_bm = Proc.new do |b,thx|
    Math::atan(Math::sin(b*thx) *  Math::tan(-36.degrees) )
  end

… you are creating a reference to point at a new empty Array instance, and immediately afterward reassigning it to point at a Proc instance (that returns a Float object.)


Since you are using the Math methods so much, it would make since to include the Math within your plugin module, so you need not have to qualify each Math method call …

module JimDavis
  module ThisPlugin

    include self
    include Math

    def vector_b(a,b,thx)

      mod = Sketchup.active_model 
      ent =  mod.active_entities

      pt_b11 = Geom::Point3d.new( 0.0 , 0.0 , 0.0 )

      pt_b12 = Geom::Point3d.new(
        a * sin(thx),
        sin(b*thx) * r,
        thx * cos(30)
      )

      # subtracting two Point3d points from each other
      # gives a vector of the distance between the two points.
      pt_b12 - pt_b11
    end

    # ... more code ...

  end
end

Now that I’m at the computer, I see several patterns that suggest you don’t quite understand Ruby yet and are perhaps carrying over code patterns from some other programming language that don’t work as you expect in Ruby. Below are some examples. If you could explain what you thought these snippets would do, perhaps we can advise you how they can be fixed.

As @DanRathbun noted, there are repeated places where you assign a variable to refer to Array.new and then immediately assign the same variable to refer to Proc.new, as

angl_a = Array.new
angl_a = Proc.new do |a,thx|
  thx * a
end

What that does is to create an Array, and assign angl_a to contain a reference to it, then immediately create an anonymous Proc object and reassign angl_a to refer to it, abandoning the Array. This pattern happens so many times in your code that it leads me to suspect you think it will do something that it does not do, such as populate the Array with a value returned by the Proc? Or that you expect the interpreter to accept the same name as simultaneously referring to the Array and Proc and to determine which you mean from later context? It is indeed possible to have a scalar variable and a def’ed method use the same symbol for their names, but that’s not what your code does. Not at all the same result as

angl_a = Array.new
def angl_a(a,thx)
  thx*a
end

which creates the situation in which the symbol :angl_a names either the Array or method and the interpreter has to figure out which you mean based on context. By the way, at least some of us consider this ambiguity to be bad style because it later requires you to understand the logic by which the interpreter resolves the name instead of being obvious. There are some expressions that are truly ambiguous and a rule to resolve them, but I doubt many Ruby programmers remember what it is and even those who do must think carefully to be sure they got it right.

There are also numerous places where you have expressions such as

 pnt[a,b]  = Geom::Point3d.new #{point_b.call(a,thx)}

or

 pm.add_polygon  #{try[0]},#{try[1]},#{try[2]}

and I wonder what you thought that syntax would do. The interpreter takes the text starting with the # and running to the end of the line as a comment. Read that way it makes no sense, as the comments seem apropos of nothing. But did you think Ruby would interpolate the result of the point_b.call or the list of try values into the expression, that is, to make the expression inside #{} into the argument of Geom::Point3d.new or pm.add_polygon? If so, that is a misunderstanding: the #{…} syntax interpolates the value from the interior of the {} block only when placed inside a String literal, per

 "#{point_b.call(a,thx)}"

which creates a String containing the result of applying #to_s to the value of the expression in the brackets. Except in a String literal bounded by “”, the # always starts a comment.

This same issue happens in the places that lead to the errors about unexpected keyword end, such as

try[0]  =  (#{pnt[a,b]})

Once again, the #{…} is not inside a String literal, the only place where it interpolates a value, so the interpreter is seeing the # as starting a comment that it ignores, leaving the broken fragment

try[0]  =  (

That open parenthesis causes the parser to start searching for a corresponding close parenthesis and eventually run to the end of the file without finding it. In my editor, these errors jump out at me because they cause meaningless indentations when the editor also thinks you have started another parentheses-bounded expression that never gets closed. At the end of the file it thinks the code is still nested 10 levels deep!

Around line 219 you have

	    if a% 2 == 1
              
              tri[m]

              angle_a.call(a,thx)

What is tri[m] supposed to accomplish? What the statement actually does is to fetch the value of the m’th element of the Array referenced by tri and then discard it because nothing is assigned to refer to it. The same effective result as if you had written a line like

  2

That 2 goes nowhere and affects nothing!

Thank you very much, this has been so helpful, as you can see I am a beginner at Ruby syntax and language. I only get to work on my problem for a few months in the winter. But this will give me a great push in the right direction.
thanks for being so helpful to a beginner.
I am using Alexander C. Schreyer code editor. Do you have any suggestions for something more powerful for a beginner.**

Thanks again.

Jim Davis.