Chart type ruby-II (variable and method)
Let's assign the value received as input to an appropriate variable and output it.
name = ARGV[0]
puts name
name is a variable. It works like a container for numbers when doing complicated calculations. By the way, as I mentioned last time, ARGV [0] contains the standard input value.
Save this program as name.rb and enter a name.
> ruby name.rb Alpha
Alpha
method
Functions are much larger than variables and feel like a container with a cohesive process. In ruby it is called method. Let's make one as an example.
def hello(name)
puts "Hello #{name}."
end
name = ARGV[0]
hello(name)
The output looks like this. The entered characters are returned with Hello.
> ruby hello_method.rb Rudy
Hello Rudy.
note
-For example, in Python, when dealing with numbers, you have to set types such as int and float. However, ruby variables can store any data without declaring the type. Reference: Ruby 3.0.0 Reference Manual
Recommended Posts