However, I *think* that ARGV is already a string? so that to_s in the first line may be redundant.
Your problem was that, although gsub! (with the !) normally modifies the receiver, 'files' wasn't the receiver (to_s was), so you weren't actually changing 'files'.
Peter Bailey wrote:
···
Hello,
I'm going nuts with the simplest stuff here. I just want to take in
array data and make it into a range.
Here's my test script, called test1.rb:
files = [ARGV]
files.to_s.gsub!(/\-/, "..")
puts files
Here's my attempt, with an argument:
test1.rb im123000-im123006
However, I *think* that ARGV is already a string? so that to_s in the
first line may be redundant.
Your problem was that, although gsub! (with the !) normally modifies the
receiver, 'files' wasn't the receiver (to_s was), so you weren't
actually changing 'files'.
Cool. That worked, Jon. Thanks. I don't quite understand it, though. I
suppose you're right, that gsub was modifying to_s and not files, but,
why was that? I thought that to_s was modifying files and then gsub was
modifying that modified files. I guess I need to tame my dots, eh?
On 11/29/06, Peter Bailey <pbailey@bna.com> wrote:
[snip]
I get:
im123000-im123006
I want:
im123000..im123006
puts ARGV.to_s.gsub(/\-/, '..')
Yup. That certainly works, too. And, that's really simple. I guess I
don't see why making "files = ARGV" throws me off so. I'm just trying to
find the right mind-set for all this.
This collapses the array "ARGV" into a single string. Not a good idea if
there is more than one input argument.
files.gsub!(/\-/, "..")
puts files
OK as long as "files" is a scalar.
However, I *think* that ARGV is already a string?
No, it is an array.
so that to_s in the
first line may be redundant.
No, it is required, but for the wrong reason and it is a VBI.
Your problem was that, although gsub! (with the !) normally modifies the
receiver, 'files' wasn't the receiver (to_s was), so you weren't actually
changing 'files'.
Hello,
I'm going nuts with the simplest stuff here. I just want to take in
array data and make it into a range.
Here's my test script, called test1.rb:
files = [ARGV]
ARGV is an array, not a scalar. you want:
files = ARGV[0]
Or maybe you want to go through the ARGV values one by one.
files.to_s.gsub!(/\-/, "..")
puts files
This code works if you provide a scalar.
Please explain. Will there be more than one value on the command line?
Yes, there may be more than one argument, and, it won't be consistent as
to how many. There could be 3, 4, 5, whatever. That's why I was just
blobbing all of them into ARGV and then trying to break them down from
there.
However, I *think* that ARGV is already a string? so that to_s in the
first line may be redundant.
Your problem was that, although gsub! (with the !) normally modifies the
receiver, 'files' wasn't the receiver (to_s was), so you weren't
actually changing 'files'.
Cool. That worked, Jon. Thanks. I don't quite understand it, though. I
suppose you're right, that gsub was modifying to_s and not files, but,
why was that? I thought that to_s was modifying files and then gsub was
modifying that modified files. I guess I need to tame my dots, eh?
Some of the advice offered by Mr. Garvin was in error.
Remember these points:
1. ARGV is an array. If you want to provide more than one input argument,
you need to parse them:
ARGV.each do |arg|
# do something here
end
But you do not normally want to collapse the array into a single string
using "ARGV.to_s"
Mr. Garvin's remark about the receiver:
Your problem was that, although gsub! (with the !) normally modifies the
receiver, 'files' wasn't the receiver (to_s was), so you weren't
actually changing 'files'.
Is mistaken in this case. In fact, file.to_s.gsub!(...) will change the
value of "files" if "files" is a string. In this case it is a string.
>
> Your problem was that, although gsub! (with the !) normally modifies the
> receiver, 'files' wasn't the receiver (to_s was), so you weren't actually
> changing 'files'.
Please explain. Will there be more than one value on the command line?
Yes, there may be more than one argument, and, it won't be consistent as
to how many. There could be 3, 4, 5, whatever. That's why I was just
blobbing all of them into ARGV and then trying to break them down from
there.
You can read ARGV sequentially as in my examples, and operate on the copy
that the loop structure makes for you.
>
> Your problem was that, although gsub! (with the !) normally modifies the
> receiver, 'files' wasn't the receiver (to_s was), so you weren't actually
> changing 'files'.
Not correct. Try it.
files=["im123000-im123006"]
=> ["im123000-im123006"]
files.to_s.gsub!(/-/,'..')
=> "im123000..im123006"
files
=> ["im123000-im123006"]
Regards,
Rimantas
Thanks. But, I'm trying to design this script for users to put in
arguments. (It's an image modification script that ftps named files
from one server to another.) So, a user might type in: test1.rb
im123456-im123459 im123462 im123466
and, I'd want to capture each of those entries, or series of entries.
So, eventually, yes, I want to get to what you typed in above, but, I
have to decipher the ARGV stuff first.
>
> Your problem was that, although gsub! (with the !) normally modifies
> the receiver, 'files' wasn't the receiver (to_s was), so you weren't
> actually changing 'files'.
Not correct. Try it.
files=["im123000-im123006"]
=> ["im123000-im123006"]
files.to_s.gsub!(/-/,'..')
=> "im123000..im123006"
files
=> ["im123000-im123006"]
Because the original example used a string, and yours uses an array, it is
not a valid counterexample.
Applying ".to_s" to a string has no effect, consequently the receiver is
changed. Applying ".to_s" to anything other than a string makes a copy.