Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#1496

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'.

Reported by Chris Kampmeier · November 30th, 2008 @ 01:35 AM

State: stale
Milestone: 3.x
Assigned to: Manfred Stienstra Manfred Stienstra
Importance: none

Activity

  1. Manfred Stienstra
    Manfred Stienstra
    • 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?

    December 22nd, 2008 @ 02:21 PM

  2. Manfred Stienstra
    Manfred Stienstra
    • 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.

    December 30th, 2008 @ 12:22 PM

  3. Brandon Keepers
    Brandon Keepers

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

    March 8th, 2009 @ 04:34 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:04 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:04 PM