Truncate and regex

Hey,

New to ruby and unfamiliar with the function ‘truncate’

Also, what does this do:

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

Particularly, the regex.

Thanks!

Hi –

On Thu, 10 Jul 2008, Justin To wrote:

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


Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

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?

On Thu, Jul 10, 2008 at 4:15 PM, Justin To
[email protected]
wrote:

Thanks!

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


Paul S.
[email protected]

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:

On Thu, Jul 10, 2008 at 4:50 PM, Paul S. [email protected]
wrote:

would have the same effect?


Paul S.
[email protected]


Paul S.
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs