469,352 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

ARGV in Ruby is an array of arrays?

18
I was under the impression that each element of the ARGV arrays contains a value (non iterable). However, when I run this

Expand|Select|Wrap|Line Numbers
  1. def test(args)
  2.     args.each do |arg|
  3.         puts arg.respond_to?("each")
  4.         arg.each do |a|
  5.             puts a
  6.         end
  7.     end
  8.  
  9. if __FILE__ == $0
  10.     test(ARGV)
  11. end
  12.  
and run it with ruby test.rb 1 2 3
I can see that each element is again an array, with one element each.

Is this expected?
Dec 9 '09 #1

✓ answered by improvcornartist

Each element is actually a string, but strings also have a method 'each' so your result is expected. Documentation is here.

So in your example, ARGV is an array of 3 string elements. Each arg then would be a string. When you do arg.each, it looks at each word within the string.

2 4343
improvcornartist
303 Expert 100+
Each element is actually a string, but strings also have a method 'each' so your result is expected. Documentation is here.

So in your example, ARGV is an array of 3 string elements. Each arg then would be a string. When you do arg.each, it looks at each word within the string.
Dec 10 '09 #2
talonx
18
@improvcornartist: Thanks a lot!
It's interesting that String.each actually splits it, by default on \n.
Dec 10 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Robin Sanderson | last post: by
21 posts views Thread by Bret | last post: by
5 posts views Thread by jab3 | last post: by
25 posts views Thread by broeks | last post: by
11 posts views Thread by vicky | last post: by
2 posts views Thread by Ben Bacarisse | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.