# A couple of basic questions

**URL:** <https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485>\
**Category:** Ruby\
**Created:** [July 30, 2008, 4:40am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485 "2008-07-30T04:40:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 30, 2008, 4:40am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485/1 "2008-07-30T04:40:31Z")

</div>

Hi,

Q.1: I was processing file/dir names with the following snippet:

Find.find(wd) do |item| # Subordinate file/directory fully-qualified  
names  
if File.directory?(item)  
# Process directory name  
if item =~ /portfolio/i  
item\_saved = ’ ’ + item  
item.gsub!(/portfolio/, ‘position’)

When I saved item in item\_saved without modification, the gsub change  
both the item name as well as the saved name (because what was saved  
was a reference to item, not item’s content). So my work-around was  
to save a modified name. That doesn’t seem like “The Ruby Way”.  
What’s a better way?

Q. 2: In that same snippet, I made two substitutions:

```
  item.gsub!(/portfolio/, 'position')
  item.gsub!(/Portfolio/, 'Position')

```

That’s also a kluge. But the following didn’t compile:

```
  item.gsub!(/([pP])ortfolio/, $1 + 'osition')

```

Is there a “Ruby Way” to reduce the redundancy?

Thanks in Advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 30, 2008, 5:06am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485/2 "2008-07-30T05:06:14Z")

</div>

RichardOnRails wrote:

> Find.find(wd) do |item| # Subordinate file/directory fully-qualified  
> names

For the sake of the deity or not of your choice, use Pathname!!

> ```
> item.gsub!(/([pP])ortfolio/, $1 + 'osition')
> 
> ```

Would item.gsub!(/([pP])ortfolio/, ‘\1osition’) work?

I suspect this would work:

```
item.gsub!(/([pP])ortfolio/){ $1 + 'osition' }
```

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [July 30, 2008, 9:34am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485/3 "2008-07-30T09:34:09Z")

</div>

2008/7/30 RichardOnRails [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> item.gsub!(/portfolio/, 'position')
> 
> ```
> 
> When I saved item in item\_saved without modification, the gsub change  
> both the item name as well as the saved name (because what was saved  
> was a reference to item, not item’s content). So my work-around was  
> to save a modified name. That doesn’t seem like “The Ruby Way”.  
> What’s a better way?

Leave item alone and store the new name in a new variable:

if …  
new\_item = item.gsub(/portfolio/, ‘position’)

(notice the missing “!”)

> Q. 2: In that same snippet, I made two substitutions:
> 
> ```
> item.gsub!(/portfolio/, 'position')
> item.gsub!(/Portfolio/, 'Position')
> 
> ```
> 
> That’s also a kluge. But the following didn’t compile:
> 
> ```
> item.gsub!(/([pP])ortfolio/, $1 + 'osition')
> 
> ```

This cannot work because $1 is evaluated only once, i.e. before #gsub  
is invoked. You need something like Phlip suggested.

item.gsub /([pP])ortfolio/, ‘\1osition’

> Is there a “Ruby Way” to reduce the redundancy?

Yes.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [August 2, 2008, 7:40am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485/4 "2008-08-02T07:40:43Z")

</div>

On Jul 30, 3:33 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if File.directory?(item)
> 
> > ```
> > item.gsub!(/Portfolio/, 'Position')
> > 
> > ```
> > 
> > Is there a “Ruby Way” to reduce the redundancy?
> 
> Yes.
> 
> Cheers
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Hi Philip & Robert,

Thank you both very much for clearing the fog from my brain 🙂 BTW,  
the deity of choice for me is the null set 🙂

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [August 4, 2008, 2:45am UTC](https://www.ruby-forum.com/t/a-couple-of-basic-questions/144485/5 "2008-08-04T02:45:43Z")

</div>

On Jul 30, 3:33 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if File.directory?(item)
> 
> > ```
> > item.gsub!(/Portfolio/, 'Position')
> > 
> > ```
> > 
> > Is there a “Ruby Way” to reduce the redundancy?
> 
> Yes.
> 
> Cheers
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Hi Gentlemen,

I just want to add that I tried both your suggestions and they worked  
perfectly.

Again, many thanks,  
Richard
