Monday, February 29, 2016

Ruby warning message: (...) interpreted as grouped expression

While following along with a tutorial I was asked to write a short program that asked the user for a sentence and then a number.  After that was complete, type out the sentence backwards the number of times that was keyed in by the user. For example when typing in a string "ABCDEF GHI" three times, I would get something that looked like this...

IHG FEDCBA
IHG FEDCBA
IHG FEDCBA

Here's the code that I typed out at first which works but I did get a warning message within Aptana Studio...


print "Type any sentence: "
sent = gets.chomp
print "Type a whole number: "
num = gets.chomp.to_i

puts "here are your sentences backwards times the number you keyed in..."
puts (sent.reverse + "\n") * num

The warning message was... 

(...) interpreted as grouped expression

After doing a google search, I came across a post that read...

"...This warning is emitted when Ruby thinks that you want an argument list but wrote a grouped expression instead. The most common cause is whitespace between the name of the message and the argument list in a message..."

...which made a lot of sense.  I don't like warnings in anything I'm coding out so I changed the code on line 7 to read...


puts "#{sent.reverse}\n" * num

After the change was made, the warning in Aptana went away.

No comments:

Post a Comment