OK, I’m still extremely new to Ruby, so if my question is stupid, please
be gentle :).
The code below is an example from a ruby book, and I look at it, I
understand most of it, but what I don’t understand is why the
author/ruby-expert has \b in the regular expression (I know it’s a
backspace), but why match for a backspace especially it’s outside of the
bracket, and why the brackets? In this context, I’m confused! I’m
talking about line number 3! Any clarification or insight would be
really helpful to me. Thank you!
class WordPlay
def self.switch_pronouns(text)
text.gsub(/\b(I am|You are|I|You|Me|Your|My)\b/i) do |pronoun|
case pronoun.downcase
when “i”
“you”
when “you”
“me”
when “me”
“you”
when “i am”
“you are”
when “you are”
“i am”
when “your”
“my”
when “my”
“your”
end
end.sub(/^me\b/i, ‘i’)
end
OK, I’m still extremely new to Ruby, so if my question is stupid, please
be gentle :).
It’s not stupid at all. Regular expressions have a bit of a learning
curve, especially if one lacks the formal theory of computer languages.
The code below is an example from a ruby book, and I look at it, I
understand most of it, but what I don’t understand is why the
author/ruby-expert has \b in the regular expression (I know it’s a
backspace),
No, it’s not in this context. As Michael pointed out, it’s a word
boundary anchor, one of several zero width assertions that you can have
in your regular expression. These assertions do not contain characters
when matching but they only match in certain places, in the case of \b
at a word boundary (i.e. the character before is a word character and
the one after a non word character or the other way round).
but why match for a backspace especially it’s outside of the
bracket, and why the brackets? In this context, I’m confused! I’m
talking about line number 3! Any clarification or insight would be
really helpful to me. Thank you!
class WordPlay
def self.switch_pronouns(text)
text.gsub(/\b(I am|You are|I|You|Me|Your|My)\b/i) do |pronoun|
The capturing group in this case is not really needed but you need
grouping because of precedence rules and the presence of “|” inside the
expression. For the non capturing version you can do
Thank you guy! You guys are so helpful. I also did a search on google
and saw that you guys hit the nail right on its head. Now I understand
what \b is! It seems that if you want to search for a whole phrase,
sentence, or a word, you need to use two \b (that’s \bwhatever here\b).
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.