Which is printed out by Ruby Console after the puts inside the iterations. This is a normal behaviour. The Console always printing out what the last method returns.
Method calls in Ruby always return an object. There is no way to prevent this. So it is really a question of whether the console you are using prints this return object (the SketchUp Ruby Console window always does).
def sortedlist(array, reverse = false)
if reverse == false
array.sort {|a,b| a <=> b}
else
array.sort {|a,b| b <=> a}
end
end
disney_movies = ["Lion King", "Little Mermaid", "Lady and the Tramp", "Finding Nemo", "Toy Story", "Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"
Even better like this:
def sortedlist(array, reverse = false)
if reverse
array.sort{|a,b| b <=> a}
else
array.sort{|a,b| a <=> b}
end
end
disney_movies = ["Lion King", "Little Mermaid", "Lady and the Tramp", "Finding Nemo", "Toy Story", "Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"
Even more better to read in a forum:
def sortedlist(array, reverse = false)
if reverse
array.sort {|a,b| b <=> a}
else
array.sort {|a,b| a <=> b}
end
end
disney_movies = ["Lion King",
"Little Mermaid",
"Lady and the Tramp",
"Finding Nemo",
"Toy Story",
"Tangled"]
puts "In proper order: #{sortedlist(disney_movies)}"
puts "In backwards sort: #{sortedlist(disney_movies, true)}"
Because the interpreter was “looking past” your poor coding syntax.
But then when you added the 2nd argument to the method call, this broke the interpreter’s ability to correctly parse the statement, hence the SyntaxError.
At one time there used to be warnings spit to STDOUT when a space occurred between a method name and the opening ( of it’s parameter list. So, the lesson is don’t put these unneeded spaces between a method name and the parameter list.
The other lesson is you need to learn how to read the error messages. It told you exactly where the error was, line number and the "^" character was pointing directly at the spot of the error.
I thought the ^ was pointing to the space before the word true. I tried leaving that space out but it did not do any good. So, yes I need to learn to read the errors.