A couple of basic questions

R

RichardOnRails

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
 
P

Phlip

RichardOnRails said:
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' }
 
R

Robert Klemme

2008/7/30 RichardOnRails said:
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?

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
 
R

RichardOnRails

2008/7/30 RichardOnRails <[email protected]>:


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?

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

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
 
R

RichardOnRails

2008/7/30 RichardOnRails <[email protected]>:


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?

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

Hi Gentlemen,

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

Again, many thanks,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top