Ruby 2.4 Enumerable#sum breaks in Rails 5.1 - ruby

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

Related

Undefined method `filter' for array

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.

.pack('B*') in Ruby 1.8 vs. 2.2

I'm working with a Ruby 1.8 based gem that relies heavily on the pack method. Running the following on different Ruby versions, yields different results:
["0"].pack("B*")
In Ruby 1.8.7 I get "\000" as a result. In Ruby 2.2.0 I get "\x00". How would I go about getting the same results in 2.2 as in 1.8?

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]"

Rake Command not working after upgrade rails and ruby version

I am getting an error when i am running any sort of rake command , it is showing me
rake aborted!
no such file to load -- config/environment
I am trying to upgrade the ruby version from ruby 1.8.7 to ruby 1.9.2 and rails 2.3.11 to rails 3.0.9 and when i am trying to start the server it is showing me
Value assigned to config.time_zone not recognized.Run "rake -D time" for a list of tasks for finding appropriate time zone names. (RuntimeError)
I am using RVM for this upgrade
ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
rails -v
Rails 3.0.9
You can't just upgrade from Rails 2 to 3 without some rather extensive preparation. All hell will break loose. Check out these Railscasts for starters:
http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1
http://railscasts.com/episodes/226-upgrading-to-rails-3-part-2
http://railscasts.com/episodes/227-upgrading-to-rails-3-part-3
There might be newer resources out there. I'd also suggest to upgrade first ruby and then Rails, or vice versa, not both at the same time. Divide and conquer.
Peepcode Rails 3 Upgrade Handbook PDF
Rails Core suggestions: Plugin to run checks on your Rails 2.x/3.x to check for obvious upgrade points on the path to 3.0.

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