Open Side Menu Go to the Top
Register
Ruby: Trouble with ARGV Ruby: Trouble with ARGV

03-25-2014 , 08:00 AM
This code:

first, second, third = ARGV

puts "The script is called: #{$0}"
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"

is supposed to return this:

The script is called unpack.rb
Your first variable is: first
Your second variable is: second
Your third variable is: third

However, it isn't unpacking and returns this:

The script is called unpack.rb
Your first variable is:
Your second variable is:
Your third variable is:

I'm sure I haven't made a typing error and am completely stuck. Google hasn't helped. Any ideas?
Ruby: Trouble with ARGV Quote
03-25-2014 , 08:19 AM
btw- I'm running the file with: unpack.rb first 2nd 3rd.

unpack.rb is the file name.
Ruby: Trouble with ARGV Quote
03-25-2014 , 01:10 PM
I don't know ruby but just typed in that program for fun and it worked fine. Do you actually type in
Code:
unpack.rb first 2nd 3rd
or
Code:
ruby unpack.rb first 2nd 3rd
(which is what I typed into my mac terminal)

Your other thread suggested your Vista setup might be allowing you to run your programs in some weird way that the Learn Ruby the Hard Way author is not be aware of. Maybe that messes up your ARGV. So my suggestion is try to preface your terminal command with ruby and see if that miraculously works.
Ruby: Trouble with ARGV Quote
03-25-2014 , 01:12 PM
Yeah I'm thinking whatever he's doing is stripping out the arguments. By the time the program runs they do not exist so they have to be removed by the time the program executes.
Ruby: Trouble with ARGV Quote
03-25-2014 , 02:09 PM
First, thanks for informing be about the use of code/uncode. Someone told me not to use line numbers, but I didn't know about this. Sorry I haven't been formatting my code properly for the forum. I'll use it in the future.

I think you guys are right and it has to do with running in Vista and it's stripping out the arguments somehow. Another incentive to set up with Linux/ubuntu?

The most important thing is for me to know what ARGV does. If I understand it, when you save a code you are actually saving scripts and this ARGV allows you to call up and work with data from multiple files that have been already saved? I'm sure it can't be that simple. As long as I understand what it does I'll try not to be anal that it isn't work exactly right on my system. But I don't think I do quite understand it yet.
Ruby: Trouble with ARGV Quote
03-25-2014 , 02:26 PM
ARGV lets you pass user defined arguments (paramaters, options, values, etc.) to your program. Instead of your program asking for input from the user, they can just supply the input as an argument.

For example, let's say you have a very simple program that does nothing except say "Hello Lestat", except you want anyone to input their own name.

You have a few options here:

Option #1
Have your program ask for input from the user and then print it out. In this case you would just run your program like normal such as... $ ruby hello.rb and then your program would prompt the user for a name.

Option #2
Use arguments such as... $ ruby hello.rb Lestat. You could run the same program with $ ruby hello.rb ShoeLace and now the program would say "Hello ShoeLace" but you did not have to change the source code of the program or bother the user by having him input the name after running the program.

Using arguments are useful for passing in options and other user input to a command line app.
Ruby: Trouble with ARGV Quote
03-25-2014 , 02:41 PM
When doing a Google search, make sure you include 'ruby', 'windows', and the code words most relevant to your problem.

I searched:
windows ARGV ruby not working
and found it very illuminating.

Actually, it probably takes some experience to decide whether your platform is relevant and need to be included.
Ruby: Trouble with ARGV Quote
03-25-2014 , 03:02 PM
Thanks guys. Very good explanation Shoe Lace. It was extremely helpful.

I was actually kinda proud of myself that I was able to work (or Google) my way through every problem so far without having to post a question here. But this time I was totally stuck. You're right Alan. Including Ruby in the search turned up way more relevant information. Thanks.
Ruby: Trouble with ARGV Quote
03-25-2014 , 07:20 PM
Well, my very short programming career has stalled -lol. I am completely stumped with this.

I think Allen C is definitely right. Vista is somehow messing up ARGV.

The problem is that the ONLY way I can call a script is by:

Code:
ex1.rb
I cannot precede a file with:

Code:
 Ruby ex1.rb
Or I get: 'ruby is not recognized as an internal or external command, operable program or batch file.

If I try:

Code:
assoc .rb
I get: File association not found for extension .rb. I tried moving the folder where I'm storing my codes to the Ruby193 directory, but still nothing.

I've done all kinds of Google searches for similar problems, but they all can precede a script with Ruby and do not seem to have the problem I have. So I cannot get to their fix.

So I've really hit a wall here. The question is if it would be easier to install unbuntu, than continuing spending time trying to figure out how to fix this on Vista. Unfortunately, this is way over my head, which is a shame because I was sailing along nicely until I hit this snag. For now, I can't see how it could possibly take more time to install Linux. But I appreciate the effort in trying to help. Thanks.
Ruby: Trouble with ARGV Quote
03-25-2014 , 07:33 PM
Just install the virtual machine and ubuntu to get back to coding. Worry about anything else and other decisions later.
Ruby: Trouble with ARGV Quote
03-25-2014 , 07:53 PM
Hopefully the only problem is 'ruby' is supposed to be lower case:
Code:
ruby ex1.rb
If that wasn't it try adding ruby to your PATH variable by following the first answer here:
http://stackoverflow.com/questions/181038/ruby-on-vista

If none of that works, I guess follow Shoe Lace's suggestion. It just seems like a big step to learn a new operating system when you're just getting your feet wet.
Ruby: Trouble with ARGV Quote
03-25-2014 , 08:02 PM
It is but it's contained in a VM so it's never that steep. If he spends 3 hours troubleshooting paths in Windows and all that crap you might as well just spend 3 hours following an in depth tutorial setting linux up and getting ruby up and going.

If he plans to go with rails and web dev in the future then it'll also make sense to be proficient with linux.
Ruby: Trouble with ARGV Quote
03-25-2014 , 09:02 PM
Quote:
Originally Posted by Allen C

If that wasn't it try adding ruby to your PATH variable by following the first answer here:
http://stackoverflow.com/questions/181038/ruby-on-vista

If none of that works, I guess follow Shoe Lace's suggestion. It just seems like a big step to learn a new operating system when you're just getting your feet wet.
First, no. The problem is not with the capitalization of Ruby. I tried it both ways.

Your link seems like it might be the fix (how did you find that? I was all over that site looking stuff up). It describes everything perfectly until:

Quote:
In the top box (User variables for XXX) either find the entry for PATH, or click new to create the entry.
If there is already a PATH use semi-colons to seperate each directory listed in it
Put c:\ruby\bin (change if you installed ruby somewhere else) in.
The were two user variables for my computer, both the same.

Variable: TEMP
Value: %USERPROFILE%\AppData\Temp

I'm not sure what the link means by find the entry for the PATH. There is a lower box called system variables. I don't think I want to edit that. If I make a new path, what do I want the variable to be? Should it read like:

Variable: ANYTHING?
Value: c:\ruby193\bin

Also, does the fact I'm having such a hard time with this mean I don't have the aptitude for programming? It almost seems like learning a coding language will be much easier for me than learning all this back end stuff that I know nothing about. Through research and common sense it seems I should have been able to figure this out by myself, but not sure if I'm smart enough. I feel like I'm close though. Thanks.
Ruby: Trouble with ARGV Quote
03-25-2014 , 09:37 PM
Hang with it Lestat. As far as overcoming the learning curve on the development environment, it takes time.
Ruby: Trouble with ARGV Quote
03-25-2014 , 09:39 PM
YES!!!!!!!!!!!!!

Thank you Allen C!! That link did the trick. I must've read everything on stackover BUT that.

Seriously everyone, thanks a lot! This whole scenario does make me question whether or not I have the aptitude for this, since my own research and smarts weren't good enough to get this resolved on my own and I expect I have a lot more obstacles in front of me. But for now, I'm back at it!

Oh and just saw your post adios. I'm gonna hang in there for now.
Ruby: Trouble with ARGV Quote
03-26-2014 , 01:05 AM
Don't sweat it Lestat. If you can teach yourself high-level poker, you can handle learning to program.

You'll never stop learning and never stop feeling stupid. This path variable business isn't something that is easy to figure out on your own. Took me a very long time to learn about it.

Setting up a working programing environment in Windows is tough work, which is why others have told you to try on Linux. I personally don't think it hurts to work some of it out on Windows, but you will reach a point where enough is enough.

I'm not a programmer at work, but I use quite a few tools that are generally only found in the programmer's toolbox. It has helped me quite a bit to learn enough about Windows to get everything set up since my jobs have not offered anything else to work with.
Ruby: Trouble with ARGV Quote
03-26-2014 , 09:17 AM
I still get befuddled by all things about setting up my development environment. It's why I quickly started using linux, and eventually got a mac instead of using windows... saves yourself a world of pain.
Ruby: Trouble with ARGV Quote
03-26-2014 , 11:43 AM
Quote:
Originally Posted by Lestat
Also, does the fact I'm having such a hard time with this mean I don't have the aptitude for programming?
It just means you haven't been all that interested in computers and how they work in the past and as a result, face more of a learning curve because to be a good, versatile programmer, you have to be familiar with things under the hood. It's more of a question of interest than aptitude.

In my experience, non-tinkerers who later became good programmers are disproportionately female and/or good at math. Take that FWIW.
Ruby: Trouble with ARGV Quote
03-26-2014 , 01:02 PM
Quote:
Originally Posted by candybar
In my experience, non-tinkerers who later became good programmers are disproportionately female and/or good at math. Take that FWIW.
I'm neither of those and I do have legit concerns whether I have the aptitude for this.

I tend to be very impatient and often can't be bothered with details. Although, I've never been officially diagnosed, I think I have a form of A.D.D. On the other hand, I love working on puzzles and problems, especially those requiring logic. I may not be the sharpest knife in the drawer, but I'm stubborn, and will doggedly pursue a problem and stick with it until I arrive at the solution. And precisely because I do not have a high level of education in things like math, etc., I'm good at finding creative ways around my lack of formal knowledge.

So I honestly don't know yet. I might have a hidden talent for this field and I might not. So far, I'm enjoying the learning process. The question is, what will it do for me? I suppose I might enjoy the tinkering as a hobby, but it really comes down to whether I can make some money at it. If not, there are probably other things I'd rather put my effort towards. Time will tell.
Ruby: Trouble with ARGV Quote
03-26-2014 , 01:27 PM
Quote:
Originally Posted by Lestat
On the other hand, I love working on puzzles and problems, especially those requiring logic. I may not be the sharpest knife in the drawer, but I'm stubborn, and will doggedly pursue a problem and stick with it until I arrive at the solution. And precisely because I do not have a high level of education in things like math, etc., I'm good at finding creative ways around my lack of formal knowledge.
This sounds good. Don't waste too much time doubt yourself. If you enjoy programming, keep doing it.

Quote:
Originally Posted by Lestat
The question is, what will it do for me? I suppose I might enjoy the tinkering as a hobby, but it really comes down to whether I can make some money at it. If not, there are probably other things I'd rather put my effort towards. Time will tell.
This, on the other hand, doesn't sound quite as good. What would you like to do if you didn't have to make money?
Ruby: Trouble with ARGV Quote
03-26-2014 , 03:25 PM
Quote:
Originally Posted by candybar
This, on the other hand, doesn't sound quite as good. What would you like to do if you didn't have to make money?
My first reaction was golf? But you know what? If I had ALL the money I ever needed and lived on my own island somewhere, I think I'd still enjoy spending a good portion of my time at programming and trying to come up with new or improved apps, etc.

It's weird. I know I have some attributes that are probably detrimental to this and some that might be good. I'm going to continue on for now and just want to thank all you guys for all the help and support I've gotten so far.
Ruby: Trouble with ARGV Quote
03-26-2014 , 04:37 PM
Lestat, I have ADD and needed a way to make some money/work. We share some similarities in that way. If you want to make money off it, you're going to need to put in some time. Start a project, and see it through... it's rewarding, though will be frustrating along the way.
Ruby: Trouble with ARGV Quote
03-27-2014 , 10:03 AM
Lestat just keep doing what you're doing. Don't worry about trying to figure out whether or not programming is your "passion" before you've even dug in. The passion mindset is a recipe for unhappiness.
Ruby: Trouble with ARGV Quote

      
m