array.include? I do not seem to GET it

J

James Byrne

I have this set of constructs:

def initialize(argv)
@argv = argv
end

def start
puts @argv.class
puts @argv.size
puts @argv
@argv.include?('--update') ? puts("Includes update") : puts("No
--update")
...

which produces this output for a given input:

Array
4
'-s /doc/data/fx-noon-all-2009-04-15.xml'
'-f csv'
'-o forex_testing'
'--update'
'-t'
No --update

Now. Here are the posers that I cannot answer.

Why is #size == 4 rather than 5; and

Why is '--update' listed when printing the array but it is not found in
the same array by #include?('--update'). What is it that I am getting
wrong here?
 
J

James Byrne

P.S. This gives exactly the same output

@argv.include?("'--update'") ? puts("Includes update") : puts("No
--update")
 
R

Roger Pack

Why is #size == 4 rather than 5; and

maybe you could add a
puts @argv.inspect to see what's going on ?
=r
 
S

Sebastian Hungerecker

Am Dienstag 14 Juli 2009 18:02:19 schrieb James Byrne:
def start
puts @argv.class
puts @argv.size
puts @argv
@argv.include?('--update') ? puts("Includes update") : puts("No
--update")
...

which produces this output for a given input:

Array
4
'-s /doc/data/fx-noon-all-2009-04-15.xml'
'-f csv'
'-o forex_testing'
'--update'
'-t'
No --update

Now. Here are the posers that I cannot answer.

Why is #size == 4 rather than 5; and

arr = ["1", "2", "3\n4", "5"]
puts arr
puts "size is #{arr.size}"

will output:
1
2
3
4
5
size is 4

Meaning: if some of the strings in the array contain newlines, you can not
tell from puts' output how many items are in the array.
Why is '--update' listed when printing the array but it is not found in
the same array by #include?('--update'). What is it that I am getting
wrong here?

puts ["--update"]
will output:
--update

puts ["'--update'"]
will output:
'--output'

In other words, puts will only surround the string with quotes if the string
actually contains quotes.
Generally you should use p instead of puts for your debug output so you can
more easily tell the exact contents of the array.

HTH,
Sebastian
 
J

James Byrne

Roger said:
maybe you could add a
puts @argv.inspect to see what's going on ?
=r

puts @argv.class
puts @argv.size
puts @argv
puts @argv.inspect
@argv.include?("--update") ? puts("Includes update") : puts("No
--update")
puts @argv.index("'--update'")

Array
5
'-s /doc/data/fx-noon-all-2009-04-15.xml'
'-f csv'
'-o forex_testing'
'--update'
'-t'
["'-s /doc/data/fx-noon-all-2009-04-15.xml' ", "'-f csv' ", "'-o
forex_testing' ", "'--update' ", "'-t' "]
No --update
nil

Excellent suggestion. The trailing space ... Sigh...

Thanks!
 
J

James Byrne

Roger said:
maybe you could add a
puts @argv.inspect to see what's going on ?
=r

puts @argv.class
puts @argv.size
puts @argv
puts @argv.inspect
@argv.include?("'--update'") ? puts("Includes update") : puts("No
--update")
puts @argv.index("'--update'")

Array
5
'-s /doc/data/fx-noon-all-2009-04-15.xml'
'-f csv'
'-o forex_testing'
'--update'
'-t'
["'-s /doc/data/fx-noon-all-2009-04-15.xml' ", "'-f csv' ", "'-o
forex_testing' ", "'--update'", "'-t'"]
Includes update
3

Thank you all very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top