PatrickHoward
PatrickHoward

Reputation: 23

How does the .match method work in relation to its args?

I am learning how to program in Ruby through the website RubyMonk.com (Fantastic website from what I know so far).

I am having a bit of trouble understanding the concept of RegEx and how it works with the .match method.

'RubyMonk Is Pretty Brilliant'.match(/ ./, 9)

What are the use(s) of the arguments in .match?

Upvotes: 2

Views: 593

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118271

What are the use(s) of the arguments in .match?

Read the documentation of String#match

Converts pattern to a Regexp (if it isn’t already one), then invokes its match method on str. If the second parameter is present, it specifies the position in the string to begin the search.

Thus in your example, #match will start its work from 10th character or 9th position on-wards.

Upvotes: 0

falsetru
falsetru

Reputation: 369064

The optional parameter of the String#match is to specify the starting position from where search begin:

'RubyMonk Is Pretty Brilliant'.match(/ ./, 0)
# => #<MatchData " I">
'RubyMonk Is Pretty Brilliant'.match(/ ./, 9)
# => #<MatchData " P">
'RubyMonk Is Pretty Brilliant'.match(/ ./, 12)
# => #<MatchData " B">

Upvotes: 2

Related Questions