0

Having a very simple method saved in test.rb:

def say_good_night(name)
    result = "Good night, #{name}"
    puts result
end

How can I pass a variable to this method through terminal?
My initial thought was test.rb say_good_night("Johny)

KOFI_113
  • 61
  • 1
  • 3
  • 1
    If you just want to interact / play with your code, there's [IRB](http://ruby-doc.org/stdlib/libdoc/irb/rdoc/IRB.html) – interactive ruby. You can load a file into it via `irb -r ./test.rb` and then enter `say_good_night("Johny")` at the prompt. – Stefan Jan 14 '19 at 12:39

1 Answers1

2

ARGV is an array of string variables passed to your script. So

def say_good_night(name)
  result = "Good night, #{name}"
  puts result
end

say_good_night(ARGV[0])

or even

ARGV.each { |name| say_good_night(name) }

and call it as

ruby test.rb KOFI_113
Ursus
  • 29,643
  • 3
  • 33
  • 50