# Truncate and regex

**URL:** <https://www.ruby-forum.com/t/truncate-and-regex/142710>\
**Category:** Rails\
**Created:** [July 10, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/truncate-and-regex/142710 "2008-07-10T17:15:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 10, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/truncate-and-regex/142710/1 "2008-07-10T17:15:36Z")

</div>

Hey,

New to ruby and unfamiliar with the function ‘truncate’

Also, what does this do:

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

Particularly, the regex.

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 10, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/truncate-and-regex/142710/2 "2008-07-10T18:20:47Z")

</div>

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](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 10, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/truncate-and-regex/142710/3 "2008-07-10T23:00:26Z")

</div>

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  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Paul S.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 10, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/truncate-and-regex/142710/4 "2008-07-10T23:00:26Z")

</div>

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 🙂

On Thu, Jul 10, 2008 at 4:50 PM, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> would have the same effect?
> 
> > 
> 
> –  
> Paul S.  
> removed\_email\_address@domain.invalid

–  
Paul S.  
removed\_email\_address@domain.invalid
