Tuesday, December 1, 2015

How to combine gets.chomp and ARGV in a Ruby script

Are trying to learn Ruby? Nice!
You have just written a  script that combines gets.chomp and command line arguments via ARGV

 and you are getting this error ?

`gets': No such file or directory @ rb_sysopen

Well the solution is to modify slightly gets.chomp, it needs to be $stdin.gets.chomp

Here is a working script (example1.rb) .



# -*- coding: utf-8 -*-
puts ""
puts "Hello World (in Ruby)"

print "Insert some input here: "
user_input = $stdin.gets.chomp
print "You inserted: #{user_input} \n"

# parse 3 command line arguments...

f1, f2, f3 = ARGV

puts "First argument is: #{f1}"
puts "Second argument is: #{f2}"
puts "Third argument is: #{f3}" 




Try it! 

>> ruby example1.rb Hello1 Hello2 Hello3



No comments:

Post a Comment

Your comment will be visible after approval.