[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need a range, but not getting it. . . .

Peter Bailey

11/29/2006 5:03:00 PM

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

--
Posted via http://www.ruby-....

32 Answers

Jon Garvin

11/29/2006 5:13:00 PM

0

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


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
>
> I get:
> im123000-im123006
>
> I want:
> im123000..im123006
>
> Thanks,
> Peter
>
>


Chris Hulan

11/29/2006 5:14:00 PM

0


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

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

Paul Lutus

11/29/2006 5:15:00 PM

0

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]

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?

--
Paul Lutus
http://www.ara...

Simon Strandgaard

11/29/2006 5:27:00 PM

0

On 11/29/06, Peter Bailey <pbailey@bna.com> wrote:
[snip]
> I get:
> im123000-im123006
>
> I want:
> im123000..im123006


puts ARGV.to_s.gsub(/\-/, '..')

--
Simon Strandgaard
http://opc...

Peter Bailey

11/29/2006 5:27:00 PM

0

Jon Garvin 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?

--
Posted via http://www.ruby-....

Robert Klemme

11/29/2006 5:30:00 PM

0

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

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

Robert Klemme

11/29/2006 5:31:00 PM

0

On 29.11.2006 18:29, Robert Klemme wrote:
> On 29.11.2006 18:02, 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
>
> 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))

:-)

robert

Paul Lutus

11/29/2006 5:32:00 PM

0

Jon Garvin 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.

--
Paul Lutus
http://www.ara...

Peter Bailey

11/29/2006 5:33:00 PM

0

Simon Strandgaard wrote:
> 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.

Cheers and thanks,
Peter

--
Posted via http://www.ruby-....

Paul Lutus

11/29/2006 5:36:00 PM

0

Simon Strandgaard wrote:

> On 11/29/06, Peter Bailey <pbailey@bna.com> 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.

--
Paul Lutus
http://www.ara...