Closed jethrodaniel closed 4 years ago
Hi @kschiess! This PR fixes CI on new rubies, so it sounds like a good change to include? :)
Hi,
I will fix the two test failures, however: The change re #to_int and #to_i does make the test work, but it does not address the underlying problem. Which is: Slices are supposed to behave like Strings. That means:
Integer('abc') # => ArgumentError
Integer('123') # => 123
'abc'.to_i # => 0
I am thinking of the case where you'd parse some kind of outside world argument; you'd want to make sure that something that needs to be an Integer really is. Calling Integer() on that slice seems natural; but that has become (Ruby 2.6) a unicorn method that cannot be influenced and that behaves rather strangely for objects that have both to_int and to_i. In essence, I cannot now override any of these to obtain the desired behaviour anymore in Ruby 2.6.
That's why I decided to release parslet 2.0 soon(ish) and essentially remove this functionality. To make sure you've got something that looks like an Integer, you'll need to Integer(slice.to_s). This breaks the abstraction and I am really sad as to what Ruby has become; when will "1" == 1 as in other languages? But I see no other way of having a version of parslet that behaves the same, no matter what Ruby version it is used with.
Can you amend the PR to change the spec for Integer conversion to
lambda { Integer(slice.to_s) }.should raise_error(ArgumentError, /invalid value/)
? I'll accept the PR just after that, amending HISTORY to announce the upcoming release.
Thank you for your work and for calling my attention to this.
kaspar
Ran this on
2.6.1
, had 2 specs fail due to the Ruby version:mathn
is deprecated since 2.5 - this fixes https://github.com/kschiess/parslet/pull/192Integer()
callsto_i
ifto_int
fails, as of 2.6 - see https://github.com/ruby/ruby/blob/ruby_2_6/spec/ruby/core/kernel/Integer_spec.rb#L22-L36