!Mac OS X-10.15.7!ruby-2.7.1p83
Like Python, Ruby doesn't require type declaration Assign the received argument ARGV [0] to an appropriate variable and output it.
name = ARGV[0]
puts name
Execution result
$ ruby name.rb Rudy
Rudy
Try to add using variables
num = ARGV[0].to_i
sum = num + num
puts sum
Execution result
$ ruby name.rb 1
2
Method
A group of functions etc. will define a method This is also a bit like a Python function definition
def method name(Argument 1,Argument 2)
Process 1
Process 2
・
・
end
For example
def name(str1,str2)
puts "#{str1} #{str2}"
end
word1 = "hello"
word2 = "world"
name(word1,word2)
Execution result
$ ruby def.rb
hello world
If you want to substitute ARGV [0] and receive characters from the keyboard
def name(str1,str2)
puts "#{str1} #{str2}."
end
word1 = ARGV[0]
word2 = ARGV[1]
name(word1,word2)
Execution result
$ ruby def.rb hello world
hello world.
Chart type ruby-II (variable and method)