Ruby variables and methods

!Mac OS X-10.15.7!ruby-2.7.1p83

variable

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.

Reference article

Chart type ruby-II (variable and method)


Recommended Posts

Ruby variables and methods
Ruby variables and functions (methods)
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
Ruby standard input and various methods
[Ruby] Handle instance variables with instance methods
Variables / scope (ruby)
Ruby Learning # 15 Methods
About Ruby variables
Ruby and Gem
About Ruby methods
[Ruby] Classes and instances
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 31 Object Methods
About Ruby instance methods
Ruby inheritance and delegation
[Swift] Constants and variables
[Ruby] methods, instance methods, etc ...
[Java] Variables and types
Coding methods and refactoring principles
GraphQL Ruby and actual development
(Note) Java classes / variables / methods
[Java] Generics classes and generics methods
rails path and url methods
Ruby syntax errors and countermeasures
Use ruby variables in javascript.
Difference between variables and instance variables
Three Bit Manipulation Methods (Ruby)
About Ruby hashes and symbols
Ruby C extension and volatile
About pluck and ids methods
Java methods and method overloads
What are Ruby class methods?
About Java class variables class methods
Ruby study notes (variables & functions)
About Ruby and object model
[Ruby] Class methods, instance methods, etc.
[Programming Complete] §4 Variables and constants
About Ruby classes and instances
About instance variables and attr_ *
Creating Ruby classes and instances
Java abstract methods and classes
How to separate words in names in classes, methods, and variables
[Ruby] Difference between get and post
About Java static and non-static methods
Write Ruby methods using C (Part 1)
[Ruby] present/blank method and postfix if.
Java Primer Series (Variables and Types)
About the equals () and hashcode () methods
[Ruby] Difference between is_a? And instance_of?
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
About Ruby single quotes and double quotes
[Ruby] Classes, instance variables, instances, etc ...
How to call classes and methods
Ruby convenience methods for fledgling engineers
Organize classes, instances, and instance variables
[Ruby] then keyword and case in
[Ruby basics] split method and to_s method