Need a range, but not getting it. . .

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

I get:
im123000-im123006

I want:
im123000…im123006

Thanks,
Peter

Try…

files = [ARGV].to_s
files.gsub!(/-/, “…”)
puts 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’.

Jon G. wrote:

Try…

files = [ARGV].to_s
files.gsub!(/-/, “…”)
puts 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?

On 11/29/06, Peter B. [email protected] wrote:
[snip]

I get:
im123000-im123006

I want:
im123000…im123006

puts ARGV.to_s.gsub(/-/, ‘…’)

Simon S. wrote:

On 11/29/06, Peter B. [email protected] 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.

Cheers and thanks,
Peter

Peter B. 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

You need

files = ARGV[0].dup

ARGV is the array of paramaters
[ARGV] is an array with a single element which is also an array

Cheers

Peter B. 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]

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?

On 29.11.2006 18:02, Peter B. 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

If you have multiple args, you can do

ARGV[0]…ARGV[1]

Otherwise you can do
a, b = ARGV[0].split(’-’, 2)
seq = a…b

Cheers

robert

Paul L. wrote:

Peter B. 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]

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.

Thanks.

On 29.11.2006 18:29, Robert K. wrote:

If you have multiple args, you can do

ARGV[0]…ARGV[1]

Otherwise you can do
a, b = ARGV[0].split(’-’, 2)
seq = a…b

Even more onelinerish

Range.new(*ARGV[0].split(’-’, 2))

:slight_smile:

robert

Jon G. wrote:

Try…

files = [ARGV].to_s

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

Not correct. Try it.

Simon S. wrote:

On 11/29/06, Peter B. [email protected] wrote:
[snip]

I get:
im123000-im123006

I want:
im123000…im123006

puts ARGV.to_s.gsub(/-/, ‘…’)

Not if there is more than one command-line argument. In such a case,
“ARGV.to_s” collapses multiple arguments into one continuous string.

Peter B. wrote:

puts ARGV.to_s.gsub(/-/, ‘…’)

Yup. That certainly works, too. And, that’s really simple.

And it is wrong. “ARGV.to_s” collapses an array into a single string. If
you
provide more than one input argument, you will see the problem.

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.

The “right” mind-set is to process the array one element at a time:

ARGV.each do |arg|

do something here

end

Paul L. wrote:

Peter B. wrote:

puts ARGV.to_s.gsub(/-/, ‘…’)

Yup. That certainly works, too. And, that’s really simple.

And it is wrong. “ARGV.to_s” collapses an array into a single string. If
you
provide more than one input argument, you will see the problem.

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.

The “right” mind-set is to process the array one element at a time:

ARGV.each do |arg|

do something here

end

Thanks, Paul. And, I’ve tried that:

files = ARGV
files.each do |f|
f = f.to_s
f.gsub!(/-/, “…”)
puts f
end

I get a “can’t modify frozen string” error message.

Peter B. wrote:

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

Not correct. Try it.

files=[“im123000-im123006”]
=> [“im123000-im123006”]

files.to_s.gsub!(/-/,’…’)
=> “im123000…im123006”

files
=> [“im123000-im123006”]

Regards,
Rimantas

Peter B. wrote:

/ …

Thanks, Paul. And, I’ve tried that:

files = ARGV
files.each do |f|
f = f.to_s
f.gsub!(/-/, “…”)
puts f
end

I get a “can’t modify frozen string” error message.

  1. “f” is already a string, it doesn’t need “.to_s”.

  2. I ran your code without error, Ruby 1.8.4. Are you sure you posted
    the
    same code you are testing?

Rimantas L. wrote:

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.

Rimantas L. wrote:

=> “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.

Peter B. wrote:

/ …

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.