Ruby The Hard Way Exercise 13 ARGV not working in PowerShell

292 Views Asked by At

I am using:

  • ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32]
  • Notepad ++
  • PowerShell v. 1.0 on Windows

When I run the code below in PowerShell:

first, second, third = ARGV

puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"

I get:

Your first variable is:
Your second variable is:
Your third variable is:

I've tried copying the code from the next exercises with ARGV and I'm getting the same error.

I would love to go forward, but if its not working i will be stuck in this book or the next one. Thank you for your time!

1

There are 1 best solutions below

3
On

I think your problem is that you did NOT give the script arguments in your command line call. The whole point of ARGV is to call the script with parameters in the command line such that the script can use the parameters. You can find more infos about ARGV here : What's the point of ARGV in Ruby?

When running in ubuntu I get a good result :

$ ruby file.rb foo bar baz
Your first variable is: foo
Your second variable is: bar
Your third variable is: baz

So, my solution is in powershell to execute the script with arguments (because otherwise they are empty, giving the result you had) :

ruby your_file.rb foo bar baz

For info, my Ubuntu version (where your script works perfectly) is :

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:    14.04
Codename:   trusty