Ruby API sub and gsub error?

So I am looking at the Ruby API for sub and gsub.
I copied and pasted
"hello".sub(%r[aeiou]/, '*')
"hello".sub(%r([aeiou])/, '<\1>')

and to my surprise, they both result in an error.

1.9.3p194 :003 > "hello".sub(%r[aeiou]/, '*')
SyntaxError: (irb):3: syntax error, unexpected ','
"hello".sub(%r[aeiou]/, '*')
                       ^
(irb):3: syntax error, unexpected ')', expecting $end
  from /home/zrun/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

Why?

···

--
Posted via http://www.ruby-forum.com/.

Hi,

There's no opening / delimiter for the %r syntax:

"hello".sub(%r/[aeiou]/, '*')

And if you're using / delimiters anyway, you can just leave out the %r:

"hello".sub(/[aeiou]/, '*')

···

--
Posted via http://www.ruby-forum.com/.

So this means there is an error in the API?

···

--
Posted via http://www.ruby-forum.com/.

Where exactly did you copy this broken example from?

I see nothing like it here Class: Regexp (Ruby 1.9.3)

···

On Sat, Jun 9, 2012 at 8:32 AM, Daniel B. <lists@ruby-forum.com> wrote:

So this means there is an error in the API?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

Hassan Schroeder wrote in post #1063853:

Where exactly did you copy this broken example from?

He already put the link up in the first post:

sub and gsub are methods of String, not of Regexp.

···

--
Posted via http://www.ruby-forum.com/\.

He already put the link up in the first post:
Class: String (Ruby 1.9.3)

? There's nothing like that in the email I'm looking at, hence my
question :slight_smile:

sub and gsub are methods of String, not of Regexp.

True, but since the question was about regex syntax, it seemed an
obvious place to look, in the absence of any other information.

In any case, the answer is apparently yes, the String doc is wrong :slight_smile:

···

On Sat, Jun 9, 2012 at 11:14 AM, Jan E. <lists@ruby-forum.com> wrote:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan