[RUBY] variable and method

!Mac OS X-10.15.7!ruby-2.7.1p83

Variable

Ruby variables do not require type declarations (int, char, etc. for C).

For example, assigning to a variable

name = 'Rudy'

You can do it with.

example

Substitute the argument ARGV [0] for the argument name

> ruby name_variable.rb Rudy
Hello Rudy.

Create a name \ _variable.rb that returns.

First, assign ARGV [0] to name \ # + begin \ _src rubyname = ARGV [0] \ # + end \ _src And \ # + begin \ _src rubyputs "Hello # {name}." \ # + end \ _src

> ruby name_variable.rb Rudy
Hello Rudy.

Is output.

method

In Ruby, a function defines a method.

method can take 0 or more arguments. For example

def hello(name)
  p name
end

example

> ruby hello_method.rb Rudy
Hello Rudy.

Create hello \ _method.rb that returns.

First, substitute ARGV [0] for name.

name = ARGV[0]

Then call the hello method

name = ARGV[0]
hello(name)

However, this gives an error because the hello method is not declared. So, create a hello method with one argument.

def hello(name)
  puts "Hello #{name}."
end
name = ARGV[0]
hello(name)

with this

> ruby hello_method.rb Rudy
Hello Rudy.

Is output.

Reference page

Chart type ruby-II (variable and method)


Recommended Posts

variable and method
ruby Exercise Memo II (variable and method)
Method
variable
[Rails] require method and permit method
Rails "render method" and "redirect method"
Java methods and method overloads
Be careful about Ruby method calls and variable references
Difference between instance method and class method
Difference between == operator and equals method
Java variable declaration, initialization, and types
[Ruby] present/blank method and postfix if.
Difference between == operator and eqals method
Difference between instance variable and class variable
[Java beginner] == operator and equals method
[Ruby basics] split method and to_s method
[Swift] Protocol concept and definition method
Java method
static variable
java (method)
Java Silver exam procedure and learning method
Overload method with Int and Integer arguments
== and equals
merge method
Difference between Ruby instance variable and local variable
Map method
include method
Abstract method
initialize method
List method
Java method
Class method
[Java] Collection and StringBuilder operation method comparison
save! method
getParameter method
[Java] method
[Introduction to Java] Variable declarations and types
A simple and convenient method for HashMap
private method
rails method
[Java] TreeMap successor method and entrySet complexity
[Java] method
[Ruby] How to use gsub method and sub method
[Ruby] "Reference to object" and "Contents of variable"
Date and time acquisition method using DateAndTime API
About call timing and arguments of addToBackStack method
Summary of Japan time setting and display method
[Rails] Difference between create method and new + save method
CRUD and resources method ~ 3 Not a secret relationship ~
[Java] Difference between "final variable" and "immutable object"
Relationship between UI test and recording, implementation method