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.
Related
This works fine in Ruby, and it should work:
puts 'don\'t'
But I want to run the same in BASH with Ruby:
%x(echo 'don\'t')
I get this error:
sh: -c: line 0: unexpected EOF while looking for matching''`
Same error occurs with ``, system(), Open3
My actual code snippet:
require 'open3'
module XdoTool
BIN = 'xdotool'
EXEC = ::ENV['PATH'].split(::File::PATH_SEPARATOR).map { |path| ::File.join(path, BIN) if ::File.executable?(::File.join(path, BIN)) }.compact.last
raise RuntimeError, "No #{BIN} found in the exported paths. Please make sure you have #{BIN} installed" unless EXEC
class << self
def type(str)
Open3.capture2("#{EXEC} type --delay 0 '#{str = str.gsub("'", '\'')}'")
str
end
end
end
# Types the quoted text anywhere.
XdoTool.type("What is the reason of the error?")
# sh: -c: line 0: unexpected EOF while looking for matching `''
# sh: -c: line 1: syntax error: unexpected end of file
XdoTool.type("What's the reason of the error?")
Please do note that the str can have anything. It can contain alphanumeric characters, symbols, emojis, or combination of all these things. How can I get around the problem with quotes here?
In shell, you simply cannot include a single quote inside a single-quoted string. It has to be in a double-quoted string. That means, if you want an argument that contains both, you need to concatenate separately quoted stings together.
echo 'He said "I can'"'"t'"'
or escape the double quotes inside a double-quoted string
echo "He said \"I can't\""
(Some shells provide yet another form of quoting that can contain an escaped single quote, namely $'He said "I can\'t"'. However, that's an extension to the POSIX standard that you can't assume is supported by the
shell Ruby will use to execute your command.)
I'm getting different error based on space between after divide(/) operator. Lets consider following example.
$ ruby -e "a /100"
-e:1: unterminated string meets end of file
-e:1: syntax error, unexpected tSTRING_END, expecting tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR
$ ruby -e "a / 100"
-e:1: undefined local variable or method `a' for main:Object (NameError)
The second example gives proper error message while the first one gives weird error. I did some research, but couldn't find out the reasons behind it. Is there anyway to fix this to give proper error messge?
If I try this with Ruby 2.3.1, I get this message: -e:1: unterminated regexp meets end of file Which seems valid since "/" is normally a regex marker...
And in the second line of your error message it says it is expecting a tREGEXP_END.
So, I think, everything is fine.
I tried this in the terminal and got the same regexp error:
$ ruby -e "a /100"
-e:1: unterminated regexp meets end of file
It seems like the /100 was interpreted as a regexp. The simple solution is to follow the correct ruby syntax (i.e. your second example: $ ruby -e "a / 100" ). Normally, it is recommended to add spaces around ruby operators, as suggest by the ruby style guide.
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.
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 ')'.
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.