syntax error-unexpected keyword else ruby - ruby

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

Related

Ruby If Elsif Else block syntax

What's wrong with this block?
if item.nil?
found = true
elsif item[:name] == name && item[:category] == category
found = true
else
i++
end
When I do syntax checking, I get
syntax error, unexpected keyword_end
but if I delete end then I get
syntax error, unexpected end-of-input
The problem is here:
i++
Ruby doesn't have a ++ operator. What you want is this:
i += 1
I believe the reason for that specific error is that Ruby is interpreting the second + as a unary + operator, i.e. the "positive number sign" (because it's the only thing that makes sense in that context), and so expects the next token to be a number†, e.g. (parentheses for clarity):
i + (+5)
...but the next token is end, ergo the syntax error.
†Or something else that responds to the +# method.

Getting an "unexpected keyword_end" for if/else statements

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

Ruby: unexpected end-of-input, expecting keyword_end for if statement

I was working with Java for a few months and am transitioning back to Ruby now. I am getting a weird error from the following code:
def count_divisors
divisor_hash = {}
25.times do |i|
divisor_hash[i] = find_dividends(i)
end
puts divisor_hash
end
def find_dividends(i)
count = 0
1000.times do |k|
if i % ( k + 1 ) == 0
count++
end
end
count
end
count_divisors()
This code is generating the following error:
test.rb:14: syntax error, unexpected keyword_end
test.rb:19: syntax error, unexpected end-of-input, expecting keyword_end
This error does not occur when I remove the if statement. I am not sure why. I know every if statement needs an end statement, for some reason it seems upset by the placement of that end statement though.
Change the count++ to count += 1 Ruby does not support incremental operator.

Ruby '{x}' block works, but 'do |x|' block does not?

I'm writing a method and for some reason it's throwing an error whenever I run an each block like this:
array.each do |i|
something_on_i
end
But, it's not throwing an error when I do the same thing like this:
array.each {|i| something_on_i}
Why? I thought the two were identical.
Here's the full code:
Working:
def factor(num)
i=2
factors=[1]
while i<=num
if (num % i == 0)
factors << i
end
i+=1
end
return factors
end
def Division(num1,num2)
facs1=factor(num1)
facs2=factor(num2)
common=[]
***facs2.each {|i| common << i if facs1.include?i}***
return common.max
end
# keep this function call here
# to see how to enter arguments in Ruby scroll down
Division(STDIN.gets)
Not working:
def factor(num)
i=2
factors=[1]
while i<=num
if (num % i == 0)
factors << i
end
i+=1
end
return factors
end
def Division(num1,num2)
facs1=factor(num1)
facs2=factor(num2)
common=[]
***facs2.each do |i|
if facs1.include?(i)
common << i
end
end***
return common.max
end
# keep this function call here
# to see how to enter arguments in Ruby scroll down
Division(STDIN.gets)
The error I get is:
(eval):334: (eval):334: compile error (SyntaxError)
(eval):323: syntax error, unexpected kDO_COND, expecting kEND
facs2.each do |i|
^
(eval):324: syntax error, unexpected tIDENTIFIER, expecting kDO or '{' or '('
(eval):334: syntax error, unexpected kEND, expecting $end
Thanks to the great help in the comments from everyone, it appears this is just an issue with Coderbyte and Repl.it, rather than an issue with the code itself. The code runs just fine in irb. #Darek Nedza pointed out that Coderbyte and Repl.it are only running Ruby 1.8.7, which is likely the problem.
Solution:
For strange errors on Repl.it or Coderbyte, just double check in irb

problem with if and else code... in ruby

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

Resources