Last active
January 19, 2016 13:18
-
-
Save YumaInaura/12c4de8e480db7affb6e to your computer and use it in GitHub Desktop.
Ruby | 正規表現の条件式に変数を使うと、名前付きキャプチャによる変数代入が出来ない ref: http://qiita.com/Yinaura/items/13bbc6b050b97139d0d2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /a (?<month>July)/ =~ 'Ruby is a July birth stone' | |
| month # => "July" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = 'a' | |
| /#{a} (?<month>July)/ =~ 'Ruby is a July birth stone' # => 8 | |
| month #=> #<NameError: undefined local variable or method `month' for main:Object> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = 'a' | |
| 'Ruby is a July birth stone'.match(/#{a} (?<month>July)/) { |matched| matched[:month] } # => July |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = 'a' | |
| month = 'Ruby is a July birth stone'.match(/#{a} (?<month>July)/) { |matched| matched[:month] } | |
| month # => "July" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - Ruby 2.2.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment