My code seems to not be working because I'm mishandling a hash...
There's two sections in my code where I reference the hash, and two distinct syntax errors that I haven't resolved through googling.
First Section Syntax Error & Code:
"syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('"
def showBoard
puts " 1 2 3"
puts " A #{#spaces["A1"]} | #{#spaces["A2"]} | #{#spaces["A3"]}"
puts ---------------------------
puts " B #{#spaces["B1"]} | #{#spaces["B2"]} | #{#spaces["B3"]}"
puts ---------------------------
puts " C #{#spaces["C1"]} | #{#spaces["C2"]} | #{#spaces["C3"]}"
puts ---------------------------
end
Second Section Syntax Error & Code:
"syntax error, unexpected =>, expecting keyword_end"
def checkGame?
if
"A1"=>"X" && "A2"=>"X" && "A3"=>"X" ||
"B1"=>"X" && "B2"=>"X" && "B3"=>"X" ||
"C1"=>"X" && "C2"=>"X" && "C3"=>"X" ||
"A1"=>"X" && "B1"=>"X" && "C1"=>"X" ||
"A2"=>"X" && "B2"=>"X" && "C2"=>"X" ||
"A3"=>"X" && "B3"=>"X" && "C3"=>"X" ||
"A1"=>"X" && "B2"=>"X" && "C3"=>"X" ||
"A3"=>"X" && "B2"=>"X" && "C1"=>"X"
puts player1 + " wins!"
checkGame == true
elsif
"A1"=>"O" && "A2"=>"O" && "A3"=>"O" ||
"B1"=>"O" && "B2"=>"O" && "B3"=>"O" ||
"C1"=>"O" && "C2"=>"O" && "C3"=>"O" ||
"A1"=>"O" && "B1"=>"O" && "C1"=>"O" ||
"A2"=>"O" && "B2"=>"O" && "C2"=>"O" ||
"A3"=>"O" && "B3"=>"O" && "C3"=>"O" ||
"A1"=>"O" && "B2"=>"O" && "C3"=>"O" ||
"A3"=>"O" && "B2"=>"O" && "C1"=>"O"
puts player2 + " wins!"
return true
checkGame == true
elsif
"A1"!=" " && "A2"!=" " && "A3"!= " " &&
"B1"!=" " && "B2"!=" " && "B3"!= " " &&
"C1"!=" " && "C2"!=" " && "C3"!= " "
puts "It's a draw. :/ "
checkGame == true
end
return false
end
What's going wrong?
The problem with the first section is
puts -------
You probably meant to enclose the dashes in quotes, to make it a string.
The => (hashrocket) operator is only used when declaring a new hash (for example {"a" => 1}) but your second bit of code is using it outside of that context ( I'm not sure what you were trying to do so can't really suggest anything). There's a few other things that don't make sense too - you're comparing string literals at the bottom , and I suspect that the precedent of || and && won't do what you want, whatever that is
Related
I have a query where user can have 3 option value where they can either just choose one on each option or 2 of either option of even 3 of the option. However, when creating the if else statement in the controller, the if else comes up until 7 statements. Any ideas how to reduce the if else statement.
each if else statement gives different input inside,
as an example
if (1 !="" && 2 =="" && 3==""){
}elseif(1 =="" && 2 !="" && 3==""){
}elseif(1 =="" && 2 =="" && 3!=""){
}elseif(1 !="" && 2 !="" && 3 ==""){
}elseif(1 !="" && 2 =="" && 3 !=""){
}elseif(1 =="" && 2 !="" && 3 !=""){
}elseif(1 !="" && 2 !="" && 3 !=""){
}else{}
You can use nested if statements instead of this , I think it may help you on this scenario
if(1 !=""){
if(2 != ""){
if(3 != ""){
// all statements are true
}else {
// 1 & 2 statements are true
}
}else {
// only 1 is true
}
}else {
// all statements is false
}
OR you can do also
if (1 != "") {
if (2 != "") {
if (3 != "") {
// all statements are true
}
}
} else {
// all statements is false
}
you can use this. This is not small but it is easy to understand
if (!empty(1) && empty(2) && empty(3)){
return '1';
}
if(empty(1) && !empty(2) && empty(3)){
return '2';
}
if(empty(1) && empty(2) && !empty(3)){
return '3';
}
if(!empty(1) && !empty(2) && empty(3)){
return '1 & 2';
}
if(!empty(1) && empty(2) && !empty(3)){
return '1 & 3';
}
if(empty(1) && !empty(2) && !empty(3)){
return '2 & 3';
}
if(!empty(1) && !empty(2) && !empty(3)){
return 'all empty';
}
here i used return early patten to simplify
I've the following dtrace one-liner:
sudo dtrace -n 'syscall:::entry { #num[probefunc] = count(); }'
which prints number of syscall count by program (after hitting Ctrl-C.
How do I add filter above probe to only apply to a process by its name (e.g. php)? Similar to dtruss -n <name>.
Ok, this is fairly straight forward, since it can be checked in dtruss how the filtering is done:
$ grep -C5 NAME $(which dtruss)
syscall:::entry
/(OPT_command && pid == $target) ||
(OPT_pid && pid == PID) ||
(OPT_name && NAME == strstr(NAME, execname)) ||
(OPT_name && execname == strstr(execname, NAME)) ||
(self->child)/
{
/* set start details */
where NAME is the process name.
So the one-liner command is (replace php with your process name):
sudo dtrace -n '
inline string NAME = "php";
syscall:::entry
/(NAME == strstr(NAME, execname)) || (execname == strstr(execname, NAME))/
{ #num[probefunc] = count(); }
'
What's wrong with this code?
if(Year(rs.Fields(created_date)) == Year(Now) AND Month(rs.Fields(created_date)) == Month(Now) AND Day(rs.Fields(created_date)) >= Day(Now) +3) {
It says, wrong syntax. How will I make this correct?
The operator is And, not &&:
>> If True && False Then
>> WScript.Echo "&& is not VBScript"
>> End If
>>
Error Number: 1002
Error Description: Syntax error
>> If True And True Then
>> WScript.Echo "&& is And"
>> End If
>>
&& is And
On second look: VBScript is not Javascript (==, {}).
I get the following error from my code.
ruby -w search.rub search.rub:19: warning: mismatched indentations at 'end' with 'case' at 12 search.rub:62: syntax error, unexpected $end, expecting keyword_end
I have a feeling that it has something to do with all the ends.
#!/usr/bin/ruby
num_line = 0
NumDiccionario = 1
def checkPassword (pass)
print pass, "\t"
system("bitcoind", "walletpassphrase", pass, "20")
case $?.exitstatus
when 0
puts "You found it!#{pass}"
File.open('password.txt', 'w') do |file|
file.puts phrase + "\n"
end
exit 0
end
str_num_line = "0"
File.open('lastLine.txt', 'r') do |file2|
str_num_line = file2.gets
end
if (str_num_line.to_i > 0 )
print "Last searching stopped at line " + str_num_line + "\n"
STDOUT.flush
print "Continue from here? y/n:"
resp = gets.chomp
if (resp == "y")
num_line =str_num_line.to_i
end
end
def checkPassword (pass)
print pass, "\t"
system("bitcoind", "walletpassphrase", pass, "20")
case $?.exitstatus
when 0
puts "You found it!#{pass}"
File.open('password.txt', 'w') do |file|
file.puts phrase + "\n"
end
end
exit 0
end
if !row[0].include? 'Changed database' || !row[0].starts_with? '---' || !row[0].include? "rows affected" || !row[0].nil? || !row[0] == ""
if i do
if !row[0].include? 'Changed database'
it works well but if i do multiple conditions then it fails on this error
SyntaxError: /Users/tamer/Sites/active/app/models/account.rb:42: syntax error, unexpected tSTRING_BEG, expecting kTHEN or ':' or '\n' or ';'
...ase' || !row[0].starts_with? '---' || !row[0].include? "rows...
Sometimes the parser can't guess at how you're grouping arguments.
In your example, it's interpreting 'Changed database' || !row[0].starts_with? as the argument passed to include?, and is choking when it comes across the next token, '---', which then makes no sense.
Adding parentheses to clear up the ambiguity will solve the problem, e.g.:
if !row[0].include?('Changed database') || !row[0].starts_with?('---') || !row[0].include?("rows affected") || !row[0].nil? || !row[0] == ""
If you really, really hate parentheses, you could also switch to using or instead of ||, which has a weaker precedence and will be applied later, e.g.:
if !row[0].include? 'Changed database' or !row[0].starts_with? '---' or !row[0].include? "rows affected" or !row[0].nil? or !row[0] == ""