This time
Hello world
I would like to create a program that outputs.
The method of issuing string with ruby is as follows.
method | effect |
---|---|
Normal output(No line breaks) | |
puts | Normal output(With line breaks) |
printf | Same as C printf |
p | debug output during coding |
pp | p print of p |
The program outputs hello world in the order of puts, p, pp, printf, print.
The contents of the source code are as follows.
hello_world.rb
puts 'hello world '
p 'hello world by '
pp 'hello world by '
printf 'hello world '
print 'hello world '
When you do this
hello world
"hello world by "
"hello world by "
hello world hello world
Was output.
Theme
> ruby hello_name.rb Ruby
If you are typed in
hello Ruby
Apply the ruby code that outputs.
puts ARGV[0]
ARGV is an argument vector, that is, an "array of arguments". It is used to pass arguments to the script when executing the script from the command line. The first argument is set in ARGV [0].
The following methods are available for outputting "hello Ruby" using this.
puts | puts "hello"+ARGV[0] |
---|---|
puts | puts "hello " #{ARGV[0]} |
print "hello #{ARGV[0]}\n" | |
print "hello "+ARGV[0] +"\n" |
From here, try executing the following code.
hello_name.rb
puts "hello #{ARGV[0]}"
puts "hello #{ARGV[0]}\n"
puts "hello " + ARGV[0] + "\n"
On the terminal
> ruby hello_name.rb Ruby
If you enter, the execution result will be
hello Ruby
hello Ruby
hello Ruby
It became.
Recommended Posts