Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active January 19, 2016 13:18
Show Gist options
  • Select an option

  • Save YumaInaura/12c4de8e480db7affb6e to your computer and use it in GitHub Desktop.

Select an option

Save YumaInaura/12c4de8e480db7affb6e to your computer and use it in GitHub Desktop.
Ruby | 正規表現の条件式に変数を使うと、名前付きキャプチャによる変数代入が出来ない ref: http://qiita.com/Yinaura/items/13bbc6b050b97139d0d2
/a (?<month>July)/ =~ 'Ruby is a July birth stone'
month # => "July"
a = 'a'
/#{a} (?<month>July)/ =~ 'Ruby is a July birth stone' # => 8
month #=> #<NameError: undefined local variable or method `month' for main:Object>
a = 'a'
'Ruby is a July birth stone'.match(/#{a} (?<month>July)/) { |matched| matched[:month] } # => July
a = 'a'
month = 'Ruby is a July birth stone'.match(/#{a} (?<month>July)/) { |matched| matched[:month] }
month # => "July"
- Ruby 2.2.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment