From: tim@... Date: 2018-08-09T04:03:55+00:00 Subject: [ruby-core:88361] [Ruby trunk Feature#14869] Proposal to add Hash#=== Issue #14869 has been updated by timriley (Tim Riley). This is looking like a really positive improvement, thank you! Would you consider taking this one step further and supporting the explicit converter, `#to_h` instead of (or as well as, if required) the implicit `#to_hash` converter? This would allow using `Hash#===` to match against e.g. `Struct` instances (which have `#to_h` but not `#to_hash`) plus any other kind of object that doesn't want to pretend to "be" a hash, but rather provide the interface for converting to one. There are penalties for implementing `#to_hash`, like implicit destructuring when an object is passed to a method with kwrest params, so its fair to expect that not every class would want to do it. `#to_h`, on the other hand, is much more common (just like we see in the example of Ruby's own `Struct`), so supporting that would make this matcher even more flexible, usable, and powerful. ---------------------------------------- Feature #14869: Proposal to add Hash#=== https://bugs.ruby-lang.org/issues/14869#change-73391 * Author: osyo (manga osyo) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- ## ������ `Hash#===` ��������������������������������������� ## ������ ��������������������������������������������������������������������������� `#===` ������������������������������������ `true` ��������������������������������� `false` ������������ ��������������������������������������������������������������������������������������� `true` ��������������������������������� `false` ������������ ```ruby user = { id: 1, name: "homu", age: 14 } # name ��������� data ��������������� true p ({ name: "homu" } === user) # => true # ������������������������������ OK p ({ id: 1, name: "homu", age: 14 } === user) # => true # name ��������� user ��������������������������������� false p ({ name: "name" } === user) # => false # ��������������������������������������� false p ({ number: 42 } === user) # => false # 1������������������������������������������ false p ({ id: 1, name: "homu", number: 42 } === user) # => false # ��������������������������������������� false p ({} == user) # => false # ������������������������������ true p ({} == {}) # => true # ��������������������������������� false p ({ id: 42 } == 42) # => false # #=== ��������������������������������������������������������� p ({ name: /^h/ } === user) # => true p ({ age: (1..20) } === user) # => true p ({ age: Integer } === user) # => true ``` ## ������������������ ### ��������������������� case-when ������ `===` ������������������������������������������������`Hash#===` ��������������������������������������������������������������������������������� ```ruby def validation user case user # name ��������������������������������� when { name: /^[a-z]/ } raise "������������������������������������������������������������" # age ��������������������������������� when { age: (0..20) } raise "0���20���������������������������" # ��������������������������������������������������������������������������������������� when { id: Integer, name: String, age: Integer } true else false end end # ��������������������������������� OK mami = { id: 1, name: "Mami", age: 21 } validation mami # => true # name ������������������������������������������ NG mado = { id: 2, name: "mado", age: 13 } validation mado # => ������������������������������������������������������������ (RuntimeError) # age ��� 0���20������������������ NG homu = { id: 3, name: "Homu", age: 14 } validation homu # => 0���20��������������������������� (RuntimeError) ``` ### `Enumerable#grep` `Enumerable#grep` ������������ `===` ������������������������������������������������������������������������ Hash ������������������������������������������������������������������ ```ruby data = [ { id: 1, name: "Homu", age: 13 }, { id: 2, name: "mami", age: 14 }, { id: 3, name: "Mado", age: 21 }, { id: 4, name: "saya", age: 14 }, ] # ������������������������������������ Hash ��������������������� p data.grep(name: /m/) # => [{:id=>1, :name=>"Homu", :age=>13}, {:id=>2, :name=>"mami", :age=>14}] p data.grep(age: (1..20)) # => [{:id=>1, :name=>"Homu", :age=>13}, {:id=>2, :name=>"mami", :age=>14}, {:id=>4, :name=>"saya", :age=>14}] ``` ## ������1��� `==` ��������������� `===` ��������������������� * `===` ��������������������������������������������������������������������������������������������� * `Class` ��� `Regexp`���`Proc` ��������������������������������������� * ��������� `===` ������������������������������ `==` ��������������������������� `obj.method(:==)` ��������������������������������������������������������� * ��������� `==` ������������������������������ `===` ��������������������������������������� ## ������2��� ��������������������������������������� * `Object#===` ��������������� `{} === 42` ������������������������ `false` ���������������������������`Hash#===` ��� `false` ������������������������ * `{} === {}` ��� `true` ��������������������������������������������� * ������������������������������������������������������������������������������������������������������������������ ```ruby def check n case n when {} "Empty Hash" when [] "Empty Array" when 0 "Zero" else "Not empty" end end p check({}) # => "Empty Hash" p check([]) # => "Empty Array" p check(0) # => "Zero" p check({ name: "mado" }) # => "Not empty" ``` ���������`Hash#===` ������������������������������������ ��������������������������������������������������������������������������������������������������������� ---Files-------------------------------- hash_eqq.patch (3.54 KB) hash_eqq.patch (4.43 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: