[RUBY] Method

What is a method

In a word, it makes it possible to use all the processes together. I don't think this alone makes sense, so I'll actually write the code.

We want to define a method called hello to output hello.

def hello              #When defining a method, start with def and end with end
  puts "Hello"
end

hello                  #Method call

Hello#Hi defined is output at hello

Return value

The method returns the last line.

def number
  "1"
  "2"
  "3"         
end

puts number
3              #The last 3 is the output

If you enter return, the process will stop halfway.

def number
  "1"
return  "2" 
  "3"         
end

puts number
2             #2 in the middle is output

argument

Things that can pass values ​​to methods etc. This allows you to bring in values ​​outside of the defined method. (Values ​​defined outside the basically defined method cannot be used inside the method, this range is called scope)

Formal arguments that are described and used for processing when defining a method The value that is actually assigned to the formal argument when calling the method is called the actual argument.

def method name(Formal argument)
  #processing
end

Method name(Actual argument)  #Method call
def add_number(number)  #(2) Substitute the actual argument 5 for the formal argument number
  puts number + number  #③ Since the value of number is 5, 5+5 calculations are made.
end

add_number(5)  #(1) Enter a value in the actual argument and call the defined method

Multiple arguments can be specified

def method name(First argument,Second argument)
  #processing
end

Method name(First argument,Second argument)

However, the number of actual and formal arguments must always be the same.

that's all.

Recommended Posts

Method
Java method
to_i method
java (method)
getRequestDispatcher () method
merge method
Map method
include method
Abstract method
initialize method
List method
puts method
Java method
Class method
save! method
getParameter method
[Java] method
private method
rails method
[Java] method
Ruby to_s method
Screen transition method
Binary search binary search method
Method to search
Factory Method Pattern
Exception switching method
RxSwift method chain
Rails delegate method
scan method problem
Java8 method reference
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[HTTP method PATCH]
About the method
[Java] forEach method
[Practice] Map method
[Servlet] Basic method
variable and method
Binary search method
Template Method pattern
Template Method Pattern
0-filled (non-zero) method
JavaSilver11 study method
keycloak test-skip method
Factory Method pattern
[Ruby] initialize method
Ruby build method
undefined method `** _ path'error
java8 method reference
[Java] Random method
Ruby accessor method
[Java] split method
ruby map method
Output about the method # 2
About No Method Error
Ruby Learning # 30 Initialize Method
Design pattern ~ Factory Method ~
JAVA DB connection method
abbreviation for ruby method
[Rails] devise helper method