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

kchak
  • 7,570
  • 4
  • 20
  • 31
  • What code causes this error? – mrzasa Jul 18 '19 at 09:25
  • @Amadan The entire information to recreate the error is there. Look at the first snippet that shows the error information. – kchak Jul 18 '19 at 14:06
  • Yeah, I didn’t think of checking the version either. – Amadan Jul 18 '19 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. – Cary Swoveland Jul 18 '19 at 17:26

2 Answers2

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.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
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.

Sam Kah Chiin
  • 4,375
  • 5
  • 25
  • 30