Truncate and regex

Hey,

New to ruby and unfamiliar with the function 'truncate'

Also, what does this do:

gsub(/<.*?>/, '')

Particularly, the regex.

Thanks!

gsub replaces stuff that matches the first expression with the second parameter. In this case, the second parameter is empty, so it’s replacing matches with nothing, effectively stripping them out.

The regex looks a little odd to me, but it looks like it’s trying to remove everything between angle-brackets from the string. I’m not sure about the ? though, I would have thought that

gsub(/<.*>/,‘’)

would have the same effect?

I looked further into it. Given the text:

Stuff

The version you posted will first match

, then match

, leaving you with “Stuff”.

The version I posted is greedy, and matches the whole of

Stuff

, leaving you with nothing.

I guess you want the version you posted :slight_smile:

Hi --

Hey,

New to ruby and unfamiliar with the function 'truncate'

Also, what does this do:

gsub(/<.*?>/, '')

Particularly, the regex.

gsub does global string substitution of a pattern with a replacement string.

You're absolutely going to have to start using ri if you want to learn about Ruby methods. Start with:

   ri gsub

then realize you care about the String version, and do

   ri String#gsub

and so forth.

David