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
-
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) #=> trueI would suggest the following:
def eql?(other) other.acts_like?(:string) ? @wrapped_string.eql?(other.to_s) : super(other) endThe 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 endThe 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
- 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
Attached is an updated patch that uses the implementation that Manfred Stienstra suggested.
-
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.
-
Santiago Pastorino
- State changed from open to stale
