语法错误 - 意外的关键字 else ruby
syntax error-unexpected keyword else ruby
我在尝试执行这段代码时遇到此错误
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
错误是 运行-length-encoding.rb:10: 语法错误,意外 keyword_else
++
运算符在 Ruby 中不存在。去
num += 1
我在尝试执行这段代码时遇到此错误
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
错误是 运行-length-encoding.rb:10: 语法错误,意外 keyword_else
++
运算符在 Ruby 中不存在。去
num += 1