I am beginner of Ruby programming. My program is count number of even length of words in a given string. But it's shows the following Error
我是Ruby编程的初学者。我的程序是计算给定字符串中偶数字的长度。但它显示了以下错误
Undefined method '<' for [0, 0] :Array
[0,0]:数组的未定义方法'<'
Here is My code
这是我的代码
def even(words, n)
i = 0, m = 0
while i < n do
count = count + words[i].length
if count%2 == 0 then
m = m + 1
end
i = i + 1
end
return m
end
prinnt "Enter The String:"
s = gets.chomp
words = s.split()
n = words.length
x = even(words, n)
puts x
4 个解决方案
#1
4
I think your problem is here
我想你的问题在这里
i = 0, m = 0
make it
让它
i = 0
m = 0
Edit:
编辑:
Also like Kai König said, if you call it like that it means
就像Kai Konig说的,如果你这样称呼它,它的意思就是
" now's the time".split #=> ["now's", "the", "time"]
http://ruby-doc.org/core-1.9.3/String.html#method-i-split
http://ruby-doc.org/core-1.9.3/String.html method-i-split
#2
1
Here's how I'd do it:
下面是我的做法:
'this is a string'.split.select{ |w| w.size % 2 == 0 }.size # => 3
Applying to gets
:
申请得到:
gets.chomp.split.select{ |w| w.length % 2 == 0 }.size
#3
1
The others have already explained to you what the immediate error is in your code. However, the bigger problem is that your code is just not idiomatic Ruby code.
其他人已经向您解释了代码中的即时错误是什么。然而,更大的问题是您的代码不是惯用的Ruby代码。
Idiomatic code would look something like this:
惯用代码看起来是这样的:
puts gets.split.map(&:length).count(&:even?)
And, as you can see, there is simply no way that you could even make a mistake such as the one you made.
正如你所看到的,你根本不可能犯像你所犯的那样的错误。
#4
0
Try it
试一试
def even(words, n)
i = 0
m = 0
count = 0
while i < n do
count = count + words[i].length
if count%2 == 0 then m = m + 1 end
i = i + 1
end
return m
end
print "Enter The String:"
s = gets.chomp
words = s.split("")
n = words.length
#p n
x = even(words, n)
puts x
#1
4
I think your problem is here
我想你的问题在这里
i = 0, m = 0
make it
让它
i = 0
m = 0
Edit:
编辑:
Also like Kai König said, if you call it like that it means
就像Kai Konig说的,如果你这样称呼它,它的意思就是
" now's the time".split #=> ["now's", "the", "time"]
http://ruby-doc.org/core-1.9.3/String.html#method-i-split
http://ruby-doc.org/core-1.9.3/String.html method-i-split
#2
1
Here's how I'd do it:
下面是我的做法:
'this is a string'.split.select{ |w| w.size % 2 == 0 }.size # => 3
Applying to gets
:
申请得到:
gets.chomp.split.select{ |w| w.length % 2 == 0 }.size
#3
1
The others have already explained to you what the immediate error is in your code. However, the bigger problem is that your code is just not idiomatic Ruby code.
其他人已经向您解释了代码中的即时错误是什么。然而,更大的问题是您的代码不是惯用的Ruby代码。
Idiomatic code would look something like this:
惯用代码看起来是这样的:
puts gets.split.map(&:length).count(&:even?)
And, as you can see, there is simply no way that you could even make a mistake such as the one you made.
正如你所看到的,你根本不可能犯像你所犯的那样的错误。
#4
0
Try it
试一试
def even(words, n)
i = 0
m = 0
count = 0
while i < n do
count = count + words[i].length
if count%2 == 0 then m = m + 1 end
i = i + 1
end
return m
end
print "Enter The String:"
s = gets.chomp
words = s.split("")
n = words.length
#p n
x = even(words, n)
puts x