Array.include? I do not seem to GET it

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?

P.S. This gives exactly the same output

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

–update")

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

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

Am Dienstag 14 Juli 2009 18:02:19 schrieb James B.:

Array
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

Roger P. wrote:

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

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!

Roger P. wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs