From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 5D01619C001F for ; Wed, 18 Nov 2015 08:24:04 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 0C321B5D891 for ; Wed, 18 Nov 2015 08:54:39 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 0FBE718CC7D1 for ; Wed, 18 Nov 2015 08:54:38 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6364F12048D; Wed, 18 Nov 2015 08:54:37 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 5108C120475 for ; Wed, 18 Nov 2015 08:54:33 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=lzlg4OBELuqUs//4f/mwCvN7pSY=; b=CGzn8ibdrjN46OzOB2 iFvBD+OPKyBjDkBeunOhvtnC+C/9kQnw257EARQGhmiIICP5vhdw4+7BKh4tBv1s iqlH/vzBvF80Yvj9OE1zr1NTaS8A7yXDekHIhaOaGGfjhHfQ5TOXVhw7KFaApWCc iAR0WSg/L8yZOnhlXiY9su7i8= Received: by filter0415p1mdw1.sendgrid.net with SMTP id filter0415p1mdw1.32176.564BBE3543 2015-11-17 23:54:29.650315201 +0000 UTC Received: from herokuapp.com (ec2-50-16-126-45.compute-1.amazonaws.com [50.16.126.45]) by ismtpd0006p1iad1.sendgrid.net (SG) with ESMTP id EN2kOHtySpWROvzN_icX4A Tue, 17 Nov 2015 23:54:29.690 +0000 (UTC) Date: Tue, 17 Nov 2015 23:54:29 +0000 From: hi@olivierlacan.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 46196 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 10984 X-Redmine-Issue-Author: olivierlacan X-Redmine-Issue-Assignee: akr X-Redmine-Sender: olivierlacan X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7auvvfGtUcWGiG5mWuoyvpNVx65WVqEIplLx OaBCuXb00LoP0uWBvSUZawOpv6iCbJJShro52ULdL5v9TlnTWtWXqrpNwqAyrIy1BdeN8tZ0g93q7n C24NnDH3duS+U0DyRi2LU6TNfJAZdkOcdxDx X-ML-Name: ruby-core X-Mail-Count: 71534 Subject: [ruby-core:71534] [Ruby trunk - Feature #10984] Hash#contain? to check whether hash contains other hash X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #10984 has been updated by Olivier Lacan. Ilya Vorontsov wrote: > So why one can sort this array and which result does one expect?: > > ```ruby > [{}, {a:1,b:2}, {c:3}, {a:1}, {b:2}].sort > ``` This is the actual result with 2.3.0-preview1: ```ruby RUBY_VERSION => "2.3.0" [{}, {a:1,b:2}, {c:3}, {a:1}, {b:2}].sort ArgumentError: comparison of Hash with Hash failed ``` Maybe I'm reading you too quickly or I'm too tired but did you note that `<=>` is **not** implemented on Hash? Matz made that clear and Akira Tanaka clarified it in [this response](https://bugs.ruby-lang.org/issues/10984#note-11). ---------------------------------------- Feature #10984: Hash#contain? to check whether hash contains other hash https://bugs.ruby-lang.org/issues/10984#change-54913 * Author: Olivier Lacan * Status: Closed * Priority: Normal * Assignee: Akira Tanaka ---------------------------------------- Comparing hashes seems like a common practice but there currently isn't a method to ask a hash instance whether it includes another hash instance. The most intuitive method to reach for would be `Hash#include?` but it is in fact an alias to `Hash#has_key?` What I'm looking for can be achieved with: ~~~ class Hash def contain?(other) self.merge(other) == self end end ~~~ Here's a simple demo of `#contain?` in use: ~~~ { a: true, b: false }.contain?({ a: true}) # => true { a: true, b: false }.contain?({ b: false}) # => true { a: true, b: false }.contain?({ a: false}) # => false { a: true, b: false }.contain?({ c: true}) # => false ~~~ One important note is that this method is *not checking for nested hash matches*. This may need to be addressed when the parameters include a nested hash perhaps. Thanks to Terence Lee's help, nobu created a patch for this feature last year. I've only modified the name of the method from [his original patch](https://gist.github.com/nobu/dfe8ba14a48fc949f2ed) and attached it to this issue. ---Files-------------------------------- Hash#contain_.patch (2.22 KB) -- https://bugs.ruby-lang.org/