Basic script not running as expected


#1

Hello,

Im learning ruby and running through some exercises. I have written the following script:

def picks_and_strings(num_pics, num_strings)
puts “-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-”
puts “You have #{num_pics} guitar pic(s).”
puts “You have #{num_strings} extra strings.”
puts “-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-”
end

puts “How many pics do you have?”
usr_pics = $stdin.gets.chomp
puts “How many strings do you have?”
usr_strings = $stdin.gets.chomp
picks_and_strings(usr_pics, usr_strings)

usr_pics, usr_strings = ARGV
puts = “We will now take an arguement variable”
picks_and_strings(usr_pics, usr_strings)

puts = “Now we will use hard coded variables:”
picks_and_strings(222, 333)

print = “And this is using script variables”
usr_pics = 444
usr_strings = 555

picks_and_strings(usr_pics, usr_strings)

It runs, here is the output:

sudo@ruby:~/ruby_labs$ ruby ./ex19.2.rb 420 840
How many pics do you have?
23
How many strings do you have?
41
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You have 23 guitar pic(s).
You have 41 extra strings.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You have 420 guitar pic(s).
You have 840 extra strings.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You have 222 guitar pic(s).
You have 333 extra strings.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
You have 444 guitar pic(s).
You have 555 extra strings.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

I cannot for the life of me figure out why the extra puts are not working. i.e.: “Now we will use hard coded variables:”, “And this is using script variables”

Can someone please let me know whats missing/added that is causing these puts to fail?

Much appreciation.
~Sudo


#2

It appears to be an issue with the $stdin.gets.chomp and the ARGV.

I’ve rewritten the script in a different way and it all works until I add the ARGV after the $stdin.gets.chomp.

Not sure if it matters in which order they are, but its not liking it.


#3

Hi Sudo,

It’s not the ARGV. In all puts and print statements after the line with ARGV, you need to remove the equals (=) sign:
puts = “We will now take an arguement variable”
puts “We will now take an arguement variable”

All runs fine now.