473,498 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ARGV in Ruby is an array of arrays?

18 New Member
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
2 4472
improvcornartist
303 Recognized Expert Contributor
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 New Member
@improvcornartist: Thanks a lot!
It's interesting that String.each actually splits it, by default on \n.
Dec 10 '09 #3

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

Similar topics

10
2592
by: Robin Sanderson | last post by:
Sorry in advance if this is a stupid question - I am new to C++. In the process of converting program to be run from the command line into a function to be run from another program I noticed...
21
18821
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
14
39743
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
5
3935
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
25
5815
by: broeks | last post by:
I'm trying to create an array of character arrays. I would use strings, but the function I need to feed into looks like this int execve(const char *filename, char *const argv , char *const envp...
5
3140
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
11
6026
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
17
3343
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
2
2402
by: Ben Bacarisse | last post by:
candide <candide@free.invalidwrites: argv can't be an array. It is a function parameter and arrays can't be passed to functions in C. There is indeed an array "out there" but all main can...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.