Benjamints Benjamints - 1 year ago 120
Ruby Question

calling the method 'count' returns wrong number of arguments

I have a method which takes two parameters. An integer for (max_length) and a string for (text). If the amount of characters per word in the text is >= max_length we remove that word from the array. At the end we count the remaining words in the array.

My method works well until

text.count
where I encounter
'wrong number of arguments, given 0 expected 1+'


I
know
this is because we haven't passed any arguments to
text.count
but I don't want to pass any in as I only want to count the remaining number of words left in the array.

However, if I performed a simple example of

x = ["This", "Will", "Work"]
x.count => 3


Why can't I use this example of count inside my block?

What am i doing wrong?

def timed_reading(max_length, text)
text.split.delete_if do |y|
y.length >= max_length
text.count
end
end

Answer Source

I think this is what you were trying to do

def timed_reading(max_length, text)
  text.split.delete_if { |y| y.length >= max_length }.count
end

You could just count words with length less than the max anyway

text.split.count { |y| y.length < max_length }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download