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’.
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?
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.
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.
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’.
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:
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’.
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’.
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.
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.