Ruby: syntax error, unexpected tIDENTIFIER, expecting ')' - ruby

Working on exercise #26 of Learn Ruby The Hard Way -- correcting a ficticious programmer's bad code.
I've got most of it worked out, but can't even get to testing because I keep getting this syntax error:
syntax error, unexpected tIDENTIFIER, expecting ')'
...on this line:
sentence = "All good\tthings come to those who wait."
I thought that was always the way variables were declared? Since the error was listing parens, I tried those too -- around sentence (even though it made no sense), around the string (both with and without quotes), with the equals sign, without the equals sign...I'm not really sure what the issue is here.

Not always errors are on the same lines as interpreter says ;) So it would be better if you include some adjacent lines next time. But as I found these lines are:
puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
sentence = "All god\tthings come to those who weight."
words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)
From here we see that the line before your specified line doesn't have closing parenthesis ')'.

Related

added a line to an existing shell script and

now I get a bizarre error as if I have changed/lost a closing argument such as fi, statement closure such as , or a hidden series of tabs.
This is the message I get in my err file:
./cron_run.sh: line 156: syntax error near unexpected token `else'
./cron_run.sh: line 156: ` else'
Again, I did not touch these lines. Not even close and what I added was another operation to dump a mongo collection to the backup directory. So I had:
...start of script
mongodump... # this was existing
mongodump... # this was the addition
...rest of script (about 70) lines unchanged
Key point:
the above lines worked
the code/server executed as expected
the error/crash occurred at the end of the process after execution (so the whole thing actually worked!)
I looked at the code with syntax highlighting (vim, nano) and I cannot see anything wrong with it (at least not an obvious thing such as a bracket, fi or missing back tick)!
Any suggestions?

Here document gives EOF error in Ruby IO

The following code give two errors which I am not able to resolve. Any help would be appreciated:
random.rb:10: can't find string "TEMPLATE" anywhere before EOF
random.rb:3: syntax error, unexpected end-of-input
Code:
id = 2
File.open("#{id}.json","w") do |file|
file.write <<TEMPLATE
{
"submitter":"#{hash["submitter"]}",
"quote":"#{hash["quote"]}",
"attribution":"#{hash["attribution"]}"
}
TEMPLATE
end
From the documentation (emphasis mine):
The heredoc starts on the line following <<HEREDOC and ends with the next line that starts with HEREDOC
Your code doesn't contain a line starting with TEMPLATE. If your text editor (or IDE) supports regular expressions in searches, try ^TEMPLATE.
You can either remove the spaces or if you want to keep them, change <<TEMPLATE into <<-TEMPLATE. The addition of - instructs the Ruby parser to search for an (possibly) intended TEMPLATE like you have in your code.

using .gsub to double quotes and punctuation in Ruby

I am trying to remove all double-quoted quotations in a string:
For example: "Mary said "Lookout"!?"
"Mary said "Lookout"!?" is coming from a html form with a textarea tag
<textarea id="receiver" name="receiver" class="form-control" maxlength= "1080" type="text"></textarea>
That is then put into a variable called Words. So:
Words = "Mary said "Lookout"!?"
Then I run
Words.gsub!(/[!?/A"|"/Z]/, "")
I want the output to read:
Mary said Lookout
Instead I am getting an error,
"Mary said "Lookout"!?".gsub!(/[!?/A"|"/Z]/, "")
SyntaxError: (irb):4: syntax error, unexpected tCONSTANT, expecting end-of-input "Mary said "Lookout"!?".gsub!(/[!?/A"|"/Z]/, "")
The error you are getting is because you have not escaped the speech marks. Ruby doesn't understand more than two speech marks in a row unless you tell it they're meant to be there. Try this:
"Mary said \"Lookout\"!?"
I believe there is still an issue with your gsub as well. Try that first and see if you can get further on your own.

Multiline if else ruby

I know this questions has been touched on elsewhere, but I'm slightly confused about the proper syntax for multi-line (block?) if else statements in ruby.
As an example:
if condition then
do something
do somethingelse
do yetanotherthing
done
else
do acompletelyunrelatedthing
done
I understand that the then statement is required if multiple lines are used, but is the done before the else necessary? This seems like it would break out of the if...else context. When I do include this done I get:
syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'
When I don't include it I get:
syntax error, unexpected keyword_else, expecting keyword_end
Um... there is no done keyword in Ruby. Here is the correct syntax:
if condition
# do stuff
else
# do other stuff
end
The then keyword is not needed either.
The then keyword is only used if you need to put the whole thing on one line (to separate the condition from the action to take if true):
if condition then do_something else do_something_different end
If you don't want it all one line (usually you don't), the syntax is as in Doorknob's answer.

What is "Syntax Error, unexpected tCONSTANT" error in Ruby?

I am currently on Lesson 9 in "Learn Ruby the hard way".
I have typed the the line number 6 exactly as the way its being instructed but still I am getting error while executing.
It says:
Syntax error, unexpected tCONSTANT, expecting $end
puts " Here ^ are the days : ", days
You have forgotten to close a string on a previous line. Here's the problem reproduced:
paul#paulbookpro ~ βΈ© ruby
days = "abc
puts "Here are the days"
-:2: syntax error, unexpected tCONSTANT, expecting $end
puts "Here are the days"
^
It's treating the double-quote before the word "Here" as the closing quote of the string on the previous line, and then wondering why you're using a constant called Here (token beginning with upper case letter).
The error message means that the ruby parser encountered a constant (i.e. an identifier starting with a capital letter) where it did not expect one (specifically the parser expected the file to end at that point).
Since the code you've shown does not even contain a constant, the problem is likely caused by another part of your code.

Resources