Skip to content

Instantly share code, notes, and snippets.

@fuse
Created July 3, 2012 12:39
Show Gist options
  • Save fuse/3039504 to your computer and use it in GitHub Desktop.
Save fuse/3039504 to your computer and use it in GitHub Desktop.
Ruby regex named capture
[1] pry(main)> str = "Candy bar"
=> "Candy bar"
[2] pry(main)> /(?<word>\w+)/ =~ str
=> 0
[3] pry(main)> word
=> "Candy"
[4] pry(main)> str =~ /(?<w>\w+)/
=> 0
[5] pry(main)> w
NameError: undefined local variable or method `w' for main:Object
from (pry):5:in `<main>'
[6] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment