472,096 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.