English 中文(简体)
3. 鲁 Ruby形配对——选择性模式
原标题:Ruby hash pattern matching - optional pattern

With Ruby 3 hash pattern matching, is it possible to specify that the rest contains only allowed keys? So far I only came up with this:

opts = { value: 5, limit: 10 } # valid
# opts = { value: 5 } # valid
# opts = { value: 5, foo: 10 } # not valid

case opts
in value:, **rest if rest.nil? || (rest.keys - %i[limit]).empty?
end

Something inspired by Flowtype might be in value:, limit?: or even in value: Integer, limit?: Integer

问题回答

请注意,<代码>rest.nil?永远不会成为true<>/code>,因此你根本不需要:

case opts
in value:, **rest if (rest.keys - %i[limit]).empty?
end

www.un.org/spanish/ga/president


没有任何与你试图做的工作相匹配的模式,这与我能够利用其他模式一样接近:

opts = {value: 5, limit: 10}
case opts
in value:, **rest if rest in {limit:_}|{}
  [value, rest]
end

#=> [5, {:limit=>10}]
opts = {value: 5}
case opts
in value:, **rest if rest in {limit:_}|{}
  [value, rest]
end

#=> [5, {}]
opts = {value: 5, foo: 1}
case opts
in value:, **rest if rest in {limit:_}|{}
  [value, rest]
end

#=> (irb):346:in `<main> : {:value=>5, :foo=>1}: guard clause does not return true (NoMatchingPatternError)

也许我误解了这个问题,但你可以指出多种模式,例如:

options = [{ value: 5, limit: 10 }, 
        { value: 5 },
        { value: 5, foo: 10 }]

outputs = options.map do |opts| 
  {opts: opts,
   matched: case opts
    in {value: Integer, **nil} 
       matched value 
    in {value: Integer, limit: Integer, **nil}
       matched value and limit 
    else 
       no match 
  end}
end

#=> [{:opts=>{:value=>5, :limit=>10}, :matched=>"matched value and limit"}, 
#    {:opts=>{:value=>5}, :matched=>"matched value"}, 
#    {:opts=>{:value=>5, :foo=>10}, :matched=>"no match"}]

似乎符合这一要求。

参考<代码>**nil系指“和没有其他钥匙”。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签