# A named group in a regex literal on the left of =~ becomes a local variable.
# Move any piece of that arrangement and the variable silently does not exist.

if /(?<year>\d{4})-(?<month>\d\d)/ =~ "released 2025-12"
  puts "literal on the left of =~      year=#{year.inspect} month=#{month.inspect}"
end

pattern = /(?<day>\d\d)/
pattern =~ "25"
puts "regex held in a variable        defined?(day) is #{defined?(day).inspect}"

"2026" =~ /(?<century>\d\d)/
puts "string on the left of =~        defined?(century) is #{defined?(century).inspect}"

sep = "-"
/(?<hh>\d\d)#{sep}(?<mm>\d\d)/ =~ "12-30"
puts "literal with an interpolation   defined?(hh) is #{defined?(hh).inspect}"
puts "  ...but $~ holds the match     $~[:hh] is #{$~[:hh].inspect}"
puts

label = "kept"
/(?<label>[a-z]+):/ =~ "no colon here"
puts "an existing local, no match     label is #{label.inspect}"

m = "released 2025-12".match(/(?<year>\d{4})-(?<month>\d\d)/)
puts "MatchData works in every case   m[:year] is #{m[:year].inspect}, named_captures #{m.named_captures.inspect}"
