Reputation: 23
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
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
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