4

So I am trying to solve a class problem/homework on repl.it, in ruby, and this is the error listing I'm given.

ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]

undefined method `filter' for [{:r=>1, :c=>0}, {:r=>0, :c=>1}]:Array
(repl):61:in `escape'
(repl):79:in `maze_escape'
(repl):82:in `<main>'

I can't understand the reason for this, because filter is clearly a method that is defined for the class Array, as a part of Ruby core, Here

4
  • What code causes this error?
    – mrzasa
    Jul 18, 2019 at 9:25
  • @Amadan The entire information to recreate the error is there. Look at the first snippet that shows the error information.
    – kchak
    Jul 18, 2019 at 14:06
  • Yeah, I didn’t think of checking the version either.
    – Amadan
    Jul 18, 2019 at 14:08
  • While your problem is Ruby version-related, it is not related to the fact that you are using repl.it and I can't see anyone searching on "ruby-2.5", so I suggest you remove those two tags. "ruby" alone is sufficient. "ruby-2.5" does no harm but the "repl.it" tag could cause some readers with repl.it issues to waste time reading your question. Not a big deal, obviously. Jul 18, 2019 at 17:26

2 Answers 2

14

You are using ruby version 2.5.5.

Array#filter was added to ruby version 2.6.0.

However, the method is merely an alias for Array#select - so you can use this instead, if you are unable to upgrade the ruby version right now.

Note: The documentation you linked to is for ruby version 2.6.3 (i.e. the latest, at the time of writing). You can see the (almost-identical) documentation for version 2.5.5 here.

1
  • 1
    Thanks. This worked. The version difference didn't occur to me.
    – kchak
    Jul 18, 2019 at 9:50
4

Are you using ruby 2.6?filter is only available in ruby 2.6.

If you are using version prior than 2.6, use select instead of filter.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.