Conditional inclusion of a key-value pair in a hash [closed]
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Can someone help me shorten the following method? I began with this, which I liked just fine: def self.some_hash { "foo" => "bar" } end Now I want to add an optional key. The tersest syntax I can think of is this: def self.some_hash(some_key=nil) answer = { "foo" => "bar" } answer[some_key] = "yucky, long-winded