#127 ✓resolved
Ben Klang

Ruby 1.9 and NumericalString comparisons in case statements

Reported by Ben Klang | January 16th, 2011 @ 02:17 PM | in 1.0.1

One of the unit tests that is failing with the transition to Ruby 1.9 is the NumericalString test for case statements. One of the original promises that NumericalString made is the ability to match case statements like this:

x = NumericalString.new("0123")
case x
when 123 then puts "The NumericalString is like a Fixnum"

case x
when "0123" then puts "This works for Strings too"

However this breaks with Ruby 1.9. The problem is that comparisons are now done with a call to Object#hash, and there is no way for us to know whether to return the hash of a String or Fixnum. With Ruby 1.8 the === operator was called, and we could (and did) satisfy it.

At the moment I do not see a workaround. The "answer" may be to document that you can not use Fixnum case statements with NumericalString. More research is necessary.

See: spec/voip/test_numerical_string.rb:8

Comments and changes to this ticket

  • Ben Klang

    Ben Klang February 22nd, 2011 @ 03:54 PM

    • State changed from “open” to “fixcommitted”
    • Milestone set to 1.0.1
    • Milestone order changed from “3034” to “0”

    for Ruby 1.9 users: The behavior of Ruby 1.9 and case statements has changed in a way that renders NumericalString objects incompatible with case statements. The suggested workaround is to cast the NumericalString to a string and then compare.

    obj = NumericalString.new("0987")
    case obj.to_s
    when "0987" then true
    else false

    Or, if you need to ignore the leading zero:

    case obj.to_i
    when 987 then true
    else false
  • Ben Klang

    Ben Klang February 22nd, 2011 @ 03:55 PM

    • Tag set to backward_incompatible, ruby19
  • Ben Klang

    Ben Klang March 4th, 2011 @ 11:07 AM

    • State changed from “fixcommitted” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket