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:
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:
> 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:
> 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: