Cannot figure what I am doing wrong again

Thanks for all of you alls help in getting over the confusion

1 Like

Yes but, as you’ll come to realize, SyntaxErrors confuse the interpreter.

In this case the “unexpected comma” (and 2nd argument,) is not the cause of the SyntaxError it’s the symptom.

The main error in your syntax was confusing the interpreter by separating an identifier (with a space) and what the interpreter thought was an expression enclosed in parenthesis.
In the first use, with a single argument, the expression in parenthesis was evaluated to the array disney_movies and that result passed to the method as a single argument.
But when the comma and another expression true was added, Ruby did not know how to evaluate what it though was an expression inside parenthesis.

When this space is removed, the interpreter correctly sees a method call with multiple arguments instead of a reference identifier and a separate expression.

If you had omitted the parenthesis, Ruby would not have been confused. Ie …

puts "In backwards sort: #{sortedlist disney_movies, true}"

… should have worked. (But most style guides are against method calls that do not wrap the parameter lists in parenthesis.)

But I would suggest you read the “Pick Axe” book.
It is listed in my book list in the Ruby Resources lists.

1 Like

I cannot find that in the list

pickaxe ruby - Google

Programming Ruby
By Dave Thomas and Andy Hunt

1 Like

Sorry, yes, if you look at Programming Ruby you will see it has a pick axe on the cover.

Thanks, I will look it over

I ran into trouble again
This is the script I got out of this video
Ruby Programming - 22 - Add an Item to a Hash - YouTube

novels = {
	Green_Eggs: 5,
	Harry_Potter: 5,
	Way_of_Kings: 4.
}
answer = "yes"
# adds a loop of asking the question while the answer is yes
while answer == "yes"
  puts "Would you like to add another book to your hash? (Type yes or no)"
  answer = gets.chomp
# case is in case of this do this
case answer
  when  "yes"
	puts "What book would you like to add?"
	book = gets.chomp
      if novels [book.to_syn] .nil?
	puts "What rating do you give it?"
	rating = gets.chomp
	novels [book.to_sym] = rating.to_i
	puts "#{book} has been added to your novels with a rating of #{rating}."
       else puts "That book is already in your Hash!"
       end 
  when "no"
	puts "Sounds good to me!"
  else puts "I do not understand. Goodbye."
end
end
puts "Here is what is in your hash now: #{novels}"

I am getting this error

Error: #<SyntaxError: <main>:4: syntax error, unexpected '}', expecting '('>
SketchUp:1:in `eval'

why is it expecting a ( instead of the }
am I doing the hash wrong

The problem may be with “4.”. I don’t think Ruby likes that bare “.” Try 4.0 if you want a float or just 4 if you want an integer.

2 Likes

Also the gets method will immediately returns nil -without waiting to user input- in SketchUp Ruby console (I can’t explain why), therefore you will get #<NoMethodError: undefined method chomp’ for nil:NilClass>` later on in your code.

so what do I use instead of gets.chomp

In SketchUp you would use an inputbox or HTMLdialog. Direct input from the Ruby Console to a script would often freeze SketchUp because one can’t assume the Ruby Console is even open. The Ruby Console isn’t really a terminal nor is it fully equivalent to irb (the Ruby interactive interpreter).

OK, thanks

The interpreter thinks that an instance method is going to be called upon the literal 4 using dot notation. So the error message says it is expecting an opening ( of probably an expression that evaluates to a method object.

This is once again syntax that confuses the interpreter parsing.

Avoid while and until constructs if you can.
They “open the door” to possibly getting caught it an endless loop scenario.

I second what Steve says regarding UI.inputbox or UI::HtmlDialog. And if it’s a tool input should be via the VCB (aka the edit control that usually is labeled “Measurements”.)

Do not insert spaces as you have done here, this could again confuse the interpreter.
Even worse confuse a human reader trying to understand the code.

Thanks for the links. This does seem to be more what I would need in SketchUp

You need to realize that the SketchUp Ruby console reads input and passes it to the interpreter. Methods such as gets don’t see your typed input.