0

So I'm trying to create a method that takes an Array and a letter as arguments and returns a new array of the words that contain that letter.

I have this thus far:

def finder(array, thing_to_find)
  find = array.keep_if {|x| x.include?(thing_to_find)==true}
   return find
end

When I run the program in Ruby, it's saying the .include? is not a valid block method.

Any suggestions?

1 Answer 1

3

You could write more simple way -

def finder(array, thing_to_find)
  array.select { |word| word.include? thing_to_find }
end

array will pass each element word from that array to the block in each iteration. Now word.include?(thing_to_find) will be evaluated - If it returns true, then word will be selected and stored to an temporary array, otherwise not. Once all iterations will be completed with respected to the array, then all selected elements from each block execution, which were being stored inside a temporary array, that array will be returned.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Arup, can you explain why my code doesn't run?
@Lasonic.. I don't know what did you mean. Your code is too much ugly, but for me it is working. I didn't notice any error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.