{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreidz2h3sqtshwuvktf2xzsgil5skvvnyox7qn5hcxhewfvdipv5p3e",
"uri": "at://did:plc:kfvfl6lsmxg64yzszmfotdq2/app.bsky.feed.post/3mmsgkstmhqk2"
},
"path": "/t/ruby-talk-444825-pattern-matching-as-string-passing-patters-to-functions/76779#post_1",
"publishedAt": "2026-05-26T13:35:09.000Z",
"site": "https://rubytalk.org",
"tags": [
"(click for more details)"
],
"textContent": "Hi Rubyists,\n\nis this the place to ask beginner questions?\n\nI am confused about pattern matching. It only seems to work when the key is of type symbol.\nHow would I apply pattern matching to string keys?\n\nExample\n\nrequire \"yaml\"\n\nyaml_data=\"\ncards_on_hand:\n- suite: hearts\nvalue: '9'\n- suite: hearts\nvalue: 'king'\n- suite: diamonds\nvalue: 'king'\n\"\n\ny=YAML.safe_load(yaml_data, symbolize_names: true)\nputs y.inspect\n\ny[:cards_on_hand].each do |a_card|\ncase a_card\nin { suite: String => captured_suite, value: 'king' }\nputs \"you have a king on your hand and its suite is #{captured_suite}\"\nelse\nputs \"a card of no interest #{a_card.inspect}\"\nend\nend\n\n{cards_on_hand: [{suit: \"hearts\", value: \"9\"}, {suit: \"hearts\", value: \"king\"}, {suit: \"diamonds\", value: \"king\"}]}\n\na card of no interest {suite: \"hearts\", value: \"9\"}\nyou have a king on your hand and its suite is hearts\nyou have a king on your hand and its suite is diamonds\n\nSecond question:\n\nSince everything in Ruby is an object, how do I pass patterns into a function? Even more specific, how do I handle capture when passing to a function?\n\nCheers,\nMarcus\n\n··· (click for more details)",
"title": "[ruby-talk:444825] Pattern matching as String, passing patters to functions"
}