Thanks for all of you alls help in getting over the confusion
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.
I cannot find that in the list
Programming Ruby
By Dave Thomas and Andy Hunt
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.
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.