6th class: Variables, method

Reference site

Chart type ruby-II (variable and method)

variable

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

6th class: Variables, method
Class method
10th class: Recursion
5th class: Output
7th class: if, else
Java programming (class method)
About Java class variables class methods
Difference between instance method and class method
Method
Why are class variables needed? [Java]
[Java] Object-oriented syntax --class method / argument
5th
6th
6th
9th
7th
11th
9th
7th
11th
10th
10th
[Beginner] Java method / class / external library [Note 23]
[Java] Differences between instance variables and class variables
Special Lecture on Multi-Scale Simulation: 11th (class)
Java programming (static clauses and "class variables")
[Java] Instance method, instance field, class method, class field, constructor summary
[Ruby] How to calculate the total amount using the initialize method and class variables