def load x
#maze_string = x
#maze_string_split = x.chars.to_a
string_counter = 0
y=#height
x=#width
(0..(y*2+1)).each do |n|
if #maze_string_split[counter] !=1
puts "Error in given string, wall expected"
else
#maze_array[n] = #maze_string_split[counter]
counter++
end
(0..(x*2)).each do |m|
if n==0 || n==(y*2+1) || m==(x*2)
if #maze_string_split[counter] != 1
puts "Error in given string"
else
#maze_array[n][m] = #maze_string_split[counter]
counter++
end
else
#maze_array[n][m] = #maze_string_split[counter]
counter++
end
end
end
end
I am getting the error in the title on the "end" statements at the conclusion of each if/else block. All seems well, but the errors remain. I tried looking to see if anyone else had this problem, but I can't find anything specific to my problem
Ruby does not have a ++ or -- operator.
Ruby will not parse these out correctly in that is the reason you're getting an unexpected keyword_end, it is expecting another operand.
Replace the
counter++ with counter += 1
Also, note that your variable is not called counter but string_counter
Related
i am getting this error while trying to execute this piece of code
def self.encode(string)
v=string.split("")
num=0
tmp=v[0]
s=""
for i in (0..v.count-1)
if v[i]==tmp
num++
else
s << num
s<<v[i-1]
tmp=v[i]
num=1
end
end
return s
end
the error is run-length-encoding.rb:10: syntax error, unexpected keyword_else
++ operator doesn't exist in Ruby. Go for
num += 1
EDIT: Someone pointed out that I needed to break correctly so I am editing the question
Scenario:
Please see following code:
print "UserID: "
uid = $stdin.gets.chomp
print "Password: "
pwd = $stdin.gets.chomp
usr_inp = "#{uid};#{pwd}"
login_status = -1
# login_info.txt - "#{userid};#{password}" - format
File.open(File.join(File.dirname(__FILE__), 'login_info.txt'), "r") do |f|
f.each_line do |line|
puts line
if (line.chomp == usr_inp)
login_status = 1
elsif (line.chomp != usr_inp && line.include?(uid)) #case a person inputs invalid password
login_status = 0
elsif (line.chomp != usr_inp && !(line.include?(uid))) #case a person inputs an invalid id
login_status = 2
end
end
end
if (login_status == 1)
puts "\nLogged in successfully: #{uid}"
elsif (login_status == 2)
puts "\nSorry, that Employee does not exist."
elsif (login_status == 0)
puts "\nLogin failed.\nPlease check credentials."
end
Problem:
break if (condition) exists in Ruby. But I don't waht that.
I want to do something like:
if (condition x)
(do something)
break
elsif (condition y)
(do something else)
break
else
(whatever)
end
Maybe I am not understanding how ruby code works. Whenever I try to put the break as I want to use it, it associates with the next elsif.
Please help.
It depends on what you need and where you need it.
A script like this:
condition = 1
case condition
when 1
puts 'one'
break
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end'
has a syntax error. break leaves a loop and there is no loop.
But with a loop, this works:
[1,2,3].each{|condition|
case condition
when 1
puts 'one'
break
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end'
}
puts 'very end'
The output is:
one
very end
You see, the loop is stopped.
If you want to continue the loop with the next element, you need next (sorry, I'm just not aware what break is doing really in Java - it's been a long time since my last Java program):
[1,2,3].each{|condition|
case condition
when 1
puts 'one'
next
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end %s' % condition
}
puts 'very end'
The result:
one
two
end 2
Other 3
end 3
very end
When you are not inside a loop (like in your code snippet), you may use exit (leave the program) or return (leave a method).
I want to divide two integers and then convert their result into a string. I have done this by putting the division into parentheses in an attempt to convert the result of the division into a string, instead of just the denominator. There don't seem to be any errors this way but I wanted to double check that this is proper syntax.
Note:#numer and #denom are both integers.
def redfrac
gcd = #numer.gcd(#denom)
if #denom != 0
rednumer = (#numer/gcd).to_s
reddenom = (#denom/gcd).to_s
if reddenom == "1"
puts rednumer
else
puts rednumer + "/" + reddenom
end
else
puts "Cannot divide by 0"
end
end
> (1.0 / 4.0).to_s
=> "0.25"
This syntax is quite legal.
But for this exact task you may use string interpolation:
def redfrac
gcd = #numer.gcd(#denom)
if #denom != 0
rednumer = #numer/gcd
reddenom = #denom/gcd
if reddenom == 1
puts "#{rednumer}"
else
puts "#{rednumer}/#{reddenom}"
end
else
puts "Cannot divide by 0"
end
end
I'm new to ruby, and I'm trying to recreate a script I made in python, in ruby, to learn. So far, I feel like this is pretty simple, but the program simply will not run. I'm getting this error
"syntax error, unexpected end-of-input, expecting keyword_end"
I'm not sure why, I ended my loop with an end
This is the code
#ruby version of work script
a = 0
b = 0
c = 0
i = " "
puts "Hello, please input the 3 character code."
i = gets.chomp
while i != "END"
if i == "RA1"
a += 1
if i == "RS1"
b += 1
if i == "RF4"
c += 1
if i == "END"
print "Complete"
else
puts "Please enter the 3 character code"
end
print "RA1: " + "#{a}" + "RS1: " + "#{b}" + "RF4: " + "#{c}"`
There are multiple issues with your code:
You have syntax errors. You need end after each of your if and else statements unlike python.
From your code it looks like you are looking for the if-elsif statement and not the multiple if statements because the else statement will be of the last if.
You need to put i = gets.chomp inside the while loop so that you don't go into an infinite loop.
Try something like this:
#ruby version of work script
a = 0
b = 0
c = 0
i = " "
puts "Hello, please input the 3 character code."
while i != "END"
i = gets.chomp
if i == "RA1"
a += 1
elsif i == "RS1"
b += 1
elsif i == "RF4"
c += 1
elsif i == "END"
print "Complete"
else
puts "Please enter the 3 character code"
end
end
print "RA1: " + "#{a}" + "RS1: " + "#{b}" + "RF4: " + "#{c}"
Do not treat to the variables and conditions ...
def index
end
def search
count = 1
while count < 3
if count == 1
#movie = "not found" if #code1 == nil || #code1 == ""
if #movie == ""
end
end
if count == 2
#movie = "not found" if #code1 == nil || #code1 == ""
if #movie == ""
if #code1.include? "movshare"
end
if #code1.include? "novamove"
end
end
end
count++
end
end
end
what is the problem in this code? i get an error:
syntax error, unexpected keyword_end
you have one more unnecessary 'end'. There are 9 opening clauses including def, while and if and 10 closing end
You are confusing the interpreter with your count++. ++ does not exist in Ruby. You need to use count += 1. The interpreter is probably assuming that is an expression involving addition, and expecting another operand but instead finding end