Undefined method `filter' for array - ruby

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

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.

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.

Related

Ruby 2.4 Enumerable#sum breaks in Rails 5.1

In Rails 5.1.4 using Ruby 2.3.5, I get this behavior:
>> [].sum
#> nil
I'd like to upgrade to Ruby 2.4, where Enumerable#sum is implemented natively. Testing this in IRB using Ruby 2.4.2, I get this result:
>> [].sum
#> 0
That's OK, and I can handle the different result. But going back to the Rails console in Rails 5.1.4 using Ruby 2.4.2, I get this:
>> [].sum
#> NoMethodError: undefined method `each' for nil:NilClass
However, in a newly created Rails 5.1.4 project, I don't get this error. What's going on here?
Looking at the source for the Active Support enumerable extensions it definitely seems like something odd is going on because you shouldn't have been getting the behaviour you described for Rails 5.1.4 using Ruby 2.3.5 i.e. you should have been getting 0 not nil there too.
Active Support's Array#sum checks if Ruby's own sum can be used by checking first.is_a?(Numeric). This will be false for an empty array so the call to super will call Enumerable#sum and both implementations of that have a default of 0.
Try [].method(:sum).source_location in the Rails console of your existing project to see if Array#sum is being overridden somewhere.
if that returns the expected line from active_support/c‌​ore_ext/enumerable.r‌​b then the next step will be to check [].method(:sum).super_method.source_location and see if a customised Enumerable#sum is the culprit.
I think that your application have some overhidden on sum method. Look the example below with 2 new applications using the 2 different versions of ruby
/testapp# ruby --version
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0
and the other version
/testapp# ruby --version
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-linux]
/testapp# rails --version
Rails 5.1.4
/testapp# rails c
irb(main):001:0> [].sum
=> 0

undefined method `execute_script' in page object gem

I have started to get this error when running my tests using the 1.2.2 version of the page object gem
Stacktrace
undefined method `execute_script' for <Selenium::WebDriver::Firefox::Bridge:0x00000003cbdba0> (NoMethodError)
/usr/lib64/ruby/gems/2.2.0/gems/page-object-1.2.2/lib/page-object/platforms/selenium_webdriver/element.rb:146:in `parent'
/usr/lib64/ruby/gems/2.2.0/gems/page-object-1.2.2/lib/page-object/platforms/selenium_webdriver/table_row.rb:30:in `find_index_by_title'
/usr/lib64/ruby/gems/2.2.0/gems/page-object-1.2.2/lib/page-object/platforms/selenium_webdriver/table_row.rb:15:in `[]'
It occurs when trying to get the value from a table using a method such as
left_table_element[rank][column]
NB. This had been previously working for over a year so I wonder if a new issue has been introduced?
Looking at the git blame it does seem like the line of code throwing the error in the element.rb might have been changed recently and maybe caused this regression?
The problem is due a change in Selenium-WebDriver. In version 3.0.0, the Remote::Bridge#executeScript method was renamed to #execute_script.
The Page-Object gem was updated in version 1.2.2 to use the new method. However, the gem dependencies were not updated to reflect the required version - ie Selenium-WebDriver v2.53.4 is still listed as the minimum requirement. It looks like this has already been fixed in the eventual Page-Object v2.0 release.
In the meantime, you can fix the issue by upgrading to Selenium-WebDriver v3 (assuming you do not want to downgrade Page-Object):
gem install selenium-webdriver
To fix this issue just need to update your page-object gem to 1.2.2 version
gem install -v 1.2.2 (for selenium-webdriver 3.x only)

Ruby version for Chef

Can Chef support newer versions of Ruby i.e. 2.0.0?
If not , is there a reason why?
In the documentation it specified up to 1.9.2
https://wiki.opscode.com/display/chef/System+Requirements+with+install (dead link)
Chef 11.6+ supports Ruby 2.0. Versions less than that will only work on Ruby 1.8 and 1.9.
Sources:
I work for Opscode
I use Chef on Ruby 2.0, 1.9, and 1.8
Documentation might need some updating. Chef supports ruby 2.0 according to this blogpost
http://www.opscode.com/blog/2013/09/03/chef-10-28-0-released/
Find the latest requirements at https://docs.chef.io/chef_system_requirements.html
Currently, Ruby 2.2.2 (or higher)

Programmatically getting FULL Ruby version?

I know it's possible to get the Ruby version (e.g. "1.9.3") via the RUBY_VERSION constant. However, I want to know how to go about determining the exact version (e.g.: "1.9.3-p0"). The reason is that there is a bug that was not fixed in earlier versions of Ruby 1.9.3 that is working in later versions, and I want some code in a gem I'm working on to account for this.
There is a RUBY_PATCHLEVEL constant as well. So you can get your version string as
"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
At least in the newest Ruby (2.3.0), there is also a RUBY_DESCRIPTION constant:
RUBY_DESCRIPTION
# => "ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]"

Why doesn't each_slice work?

I am trying to use the Enumerable#each_slice. It doesn't work on my computer, stating that method is not found.
I am running ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
API: http://ruby-doc.org/core/classes/Enumerable.html#M003142
Example:
(1..10).each_slice(3) {|a| p a} # I get NoMethodError: undefined method `each_slice' for 1..10:Range
What am I doing wrong?
In ruby 1.8.6 you have to require 'enumerator' (which is part of stdlib and has been merged into core in 1.8.7+) before using each_slice.
Sadly the ruby-doc lists methods that are added to core classes by stdlib without mentioning where the methods are from.
just compared 1.8.6 to 1.9 and it looks like
(1..10).respond_to? :each_slice
is true in 1.9 and false in 1.8.6. So, the doc you are using is not for 1.8.6. if you can upgrade to a newer version of Ruby easily it should give you that method on the Range.

Resources