Robert A. Heiler wrote:
The current examples are:
r1 = Regexp.new('^a-z+:\s+\w+') #=> /^a-z+:\s+\w+/
r2 = Regexp.new('cat', true) #=> /cat/i
r3 = Regexp.new(r2) #=> /cat/i
r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
As you can see, we have examples for one argument, and two arguments given
but not for three arguments.
Thanks!
Hey Robert,
TL;DR
$KCODE is deprecated and does not affect the use of Regexp at this point. I have a patch ready to go that removes [, kcode] from the doc. That being said I would be happy to modify the patch and just submit an example of using Regexp with 3 parameters.
r5 = Regexp.new('fish', Regexp::IGNORECASE, 'utf8')
The Good Stuff
The kcode parameter in new(string, [options [, kcode]]) or compile(string, [options [, kcode]]) is used to set the global $KCODE variable which basically sets the encoding to be used. This could be UTF-8, Unicode, or any other valid encoding etc.
I believe the example you are looking for would be the following:
r5 = Regexp.new('fish', Regexp::IGNORECASE, 'utf8')
This example is not dependent on the [option] Regexp::IGNORECASE. Any other accepted option should still work with the above example.
However, $KCODE was deprecated in Ruby 1.9 so you will find that setting the encoding does not do anything and you will receive a warning in most cases. You can see this here:
ruby regexp_example.rb
regexp_example.rb:3: warning: encoding option is ignored - utf8
or
irb(main):001:0> Regexp.new('fish', Regexp::IGNORECASE, 'utf8')
(irb):1: warning: encoding option is ignored - utf8
=> /fish/i
However setting the encoding at the console does not provide a warning:
ruby -KU #=> Runs Ruby with UTF-8 encoding
I went ahead and prepared a patch for the documentation. You should be able to find it attached to this comment. I think the best course of action would be to remove [, kcode] since you cannot pass any option that will actually change the outcome. The internal C code will default to ‘n’ or ‘N’ which you can pass in without warning but basically just says “don’t specify encoding just treat as binary string”. That being said since it was deprecated I would be happy to modify the patch and just add the example for now.
If you would like more information on regular expressions and kcodes in ruby
go ahead and checkout the following links. They helped a lot when I was
researching this topic.
http://nuclearsquid.com/writings/ruby-1-9-what-s-new-what-s-changed/
http://graysoftinc.com/character-encodings/the-kcode-variable-and-jcode-library
https://www.ruby-forum.com/topic/178589
http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/