From: "Dan0042 (Daniel DeLorme)" Date: 2022-01-26T19:07:45+00:00 Subject: [ruby-core:107294] [Ruby master Feature#18408] Allow pattern match to set instance variables Issue #18408 has been updated by Dan0042 (Daniel DeLorme). Eregon (Benoit Daloze) wrote in #note-17: > In practice it should rarely matter because if a clause isn't matched the code should obviously not look at variables not set by that clause. I'm not sure I agree with that. When I have `if condition; x=42; end` I expect `x` to be nil if the branch was not taken. Not some undefined semi-random value. And sometimes I depend on that behavior in my code. I understand pattern matching doesn't work that way, and I understand why and how pattern matching works the way it does, but only because I read this thread. Otherwise I would assume the variable is only set when the pattern matches successfully. So I had expected this to work and was fairly surprised that it doesn't: ```ruby case [1,2,3,4] in [*,a,1,*] puts "before one: #{a}" in [*,b,2,*] puts "before two: #{b}" in [*,c,3,*] puts "before three: #{c}" else puts "no match" end #I expected a==nil since the first pattern didn't match, but instead it's a==3 if !a puts "nothing before one" end ``` I don't know if the current behavior fits Matz' POLS, but it doesn't fit mine. (Not saying it necessarily should, but it's at least a data point for one person who finds this behavior surprising). ---------------------------------------- Feature #18408: Allow pattern match to set instance variables https://bugs.ruby-lang.org/issues/18408#change-96191 * Author: Dan0042 (Daniel DeLorme) * Status: Assigned * Priority: Normal * Assignee: ktsj (Kazuki Tsujimoto) ---------------------------------------- I expected this to work: ```ruby 42 => @v ``` But instead it raises "syntax error, unexpected instance variable" Is this intentional? -- https://bugs.ruby-lang.org/ Unsubscribe: