This project is archived and is in readonly mode.

#1496 ✓stale
Chris Kampmeier

Add #hash and #eql? to ActiveSupport::Multibyte::Chars so you can use multibyte strings as hash keys without Ruby 1.9

Reported by Chris Kampmeier | November 30th, 2008 @ 01:35 AM | in 3.x

Multibyte strings can't be used as hash keys without Ruby 1.9:


>> RUBY_VERSION
=> "1.8.7"
>> {"string".mb_chars => true} == {"string".mb_chars => true}
=> false
>> {"string".mb_chars => 3}.merge("string".mb_chars => 4).keys.map(&:to_s)
=> ["string", "string"]

In my testing, this affects both Ruby 1.8.6 and 1.8.7.

Objects used as hash keys must define #hash and #eql?, which are used during hash insertion and comparison. ActiveSupport::Multibyte::Chars should proxy those methods to the wrapped string instead of handling them itself.

This has come up quite a bit for me, since we sometimes need to use user-inputted strings as hash keys, and we like to downcase/strip them to normalize.

As far as I can tell, this works out-of-the-box on Ruby 1.9.1, so the attached patch only affects RUBY_VERSION < '1.9'.

Comments and changes to this ticket

  • Manfred Stienstra

    Manfred Stienstra December 22nd, 2008 @ 02:21 PM

    • Tag changed from patch to multibyte, patch
    • Assigned user set to “Manfred Stienstra”

    I agree that it would be great if you could use Chars instances as hash keys, there are two problems.

    The first problem is that your solution is too broad:

    
    '1'.mb_chars.eql?(1) #=> true
    

    I would suggest the following:

    
    def eql?(other)
      other.acts_like?(:string) ? @wrapped_string.eql?(other.to_s) : super(other)
    end
    

    The other problem is a limitation caused by Hash#[] optimizations for String. So even if you do the following:

    
    class String
      def eql_with_wrapped_string?(other)
        eql_without_wrapped_string?(other.acts_like?(:string) ? other.to_s : other)
      end
      alias_method_chain :eql?, :wrapped_string
    end
    

    The following will still be the case:

    
    hash = {}
    hash['string'] = 'value'
    hash['string'.mb_chars] = 'other value'
    
    hash.keys.map(&:to_s) #=> ['string', 'string']
    

    So I'm not sure what the correct course of action is. Maybe we should just tell people to coerce the Chars instance to string before using it as a key?

  • Manfred Stienstra

    Manfred Stienstra December 30th, 2008 @ 12:22 PM

    • State changed from “new” to “incomplete”

    I'm closing the ticket as incomplete for now, please pick it up again if you want this to move forward.

  • Brandon Keepers

    Brandon Keepers March 8th, 2009 @ 04:34 AM

    Attached is an updated patch that uses the implementation that Manfred Stienstra suggested.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • State changed from “incomplete” to “open”
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • State changed from “open” to “stale”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages