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.
The OP’s example is wrong anyway – it doesn’t apply to the case I
described.
tricks
to return a string. many classes do such things. one should assume only
that to_s returns a string representation of the object, whether or not
it’s a copy is class dependant.
Okay, I agree, but I limited my remark to a string object getting
“.to_s”
applied to it, while really, always, has no effect, and returns the
original object. And I see I should have said, “applying ‘.to_s’ to
anything other than a string usually makes a copy.”
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.
i wounn’t quite say that. mmap, for example, simply does some pointer
tricks
to return a string. many classes do such things. one should assume
only that
to_s returns a string representation of the object, whether or not it’s
a copy
is class dependant.
Okay, I agree, but I limited my remark to a string object getting “.to_s”
applied to it, while really, always, has no effect, and returns the
original object. And I see I should have said, “applying ‘.to_s’ to
anything other than a string usually makes a copy.”
indeed. i’ve been bitten the few times it hasn’t though!
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
I had a similar need recently in that I wanted to specify ranges and
individual numbers. I
chose the input syntax as
3-10,14,24,26-28
type of thing.
What about this?:
irb(main):016:0> class String
irb(main):017:1> def parse_range #(str)
irb(main):018:2> sa=[]
irb(main):019:2> self.split(",").each do |s|
irb(main):020:3* if s=~/([\w\d])-([\w\d])/
irb(main):021:4> sa << ($1…$2).to_a
irb(main):022:4> else
irb(main):023:4* sa << s
irb(main):024:4> end
irb(main):025:3> end
irb(main):026:2> pr=sa.flatten.uniq
irb(main):027:2> end
irb(main):028:1> end
=> nil
irb(main):029:0> input
=> “im123456-im123459,im123462,im123466”
irb(main):030:0> input.parse_range
=> [“im123456”, “im123457”, “im123458”, “im123459”, “im123462”,
“im123466”]
Not exactly the same input syntax as you have suggested you want, but
close.
Alle 19:04, giovedì 30 novembre 2006, Peter B. ha scritto:
Why use gsub! at all?
ARGV.each do |f|
puts f.gsub(/-/, “…”)
end
Thanks. Yeh, I can do a “puts,” but, I can’t just change the silly hypen
to 2 range dots. I get the frozen string error.
The point is to use puts, it is to use gsub instead of gsub!, which I
see you
used in a previous post. gsub! modifies the string it is called on,
which you
can’t do with strings in ARGV, since they’re frozen. gsub, instead,
creates a
new string, so it shouldn’t complain about frozen string.
I ran it again, too, and, I still get the “can’t modify frozen string
(TypeError)” error.
Well, rather than try to sort this out, why not make a copy of the
string
and solve it that way?
Rather than:
f.gsub!(/-/, “…”)
Do this:
f = f.gsub(/-/, “…”)
BTW there is no purpose to this copy:
files = ARGV
files.each do |f|
Just do:
ARGV.each do |f|
That did it! Here’s what I did. I need to not just turn the hyphen into
range dots, but, I also need to get rid of any commas and stuff between
input entries.
ARGV.each do |f|
f = f.gsub(/-/, “…”)
f = f.gsub(/,/, “”)
puts f
end
So, here’s my input:
test1.rb im145000-145004, im145006, im145009
I get:
im45000…im145004
im145006
im145009
Phew. Thanks, everyone. Now I’m going to work on breaking down that
range into its elements, but, I think I can handle that.
test1.rb im145000-145004, im145006, im145009
Are you putting those commas in there? This is not a good idea. It’s not
a
good idea because someone will get the notion that they can use the
commas
and not put in the spaces, and that changes everything.
For command-line arguments, this:
xxxxx yyyyy zzzzz
is not at all equal to this:
xxxxx,yyyyy,zzzzz
I think you need to decide what input format you want to allow. If you
don’t, your program won’t be able to separate the arguments in a way
that
you expect.