[Ruby] Method definition summary

[Ruby] Method definition summary

Summary of description about method definition of ruby.

The method definition method is basically similar to python. Ruby does not require ":" at the end of the first line. Need "end" at the end.

It is a keyword argument using ":", and a value can be passed by the argument name.

table of contents

  1. [Method definition](#method definition)
  2. [Call Method](#Call Method)
  3. [Pass arguments](#pass arguments)
  4. [Return value](#Return value)
  5. [Use return value in method](Use return value in # method)
  6. [Return boolean value in return value](#Return boolean value in return value)
  7. [Return ends the method](#return ends the method)
  8. [Return value and if statement (pass the return value to the if statement)](# Return value and if statement Pass the return value to the if statement)
  9. [Use return and if statements in return value and if statement methods](#Use return and if statements in return value and if statement methods)
  10. [Keyword Argument](#Keyword Argument)

Method definition


def method name

└ end required

Method call

Method name └ () Not required * When there is no argument

Method example

def hello
 puts "Hello"



Pass arguments


def method name(Argument name 1,Argument name 2,,)

Method name(Argument name 1,Argument name 2,,)

└ Cannot be called without arguments (error) └ Match the number of arguments (error if not matched) └ Argument name can be used only in the defined method (scope)

Return value

return value └ Replace the value with the method. └ Values are strings, formulas, etc.

Use the return value in the method


def method name
return value

Return value example

def divide(a,b)
  return a/b

puts add(10,5)

puts "The division result is#{value}is"

The division result is 2

Returns a boolean value as a return value

return conditional expression └ The result of the conditional expression is returned as true / false

Method name? (Argument name) └ Add "?" To the method that returns the boolean value (true / false). └ As a convention

Method that returns 0 or more as a boolean value

def positive?(value)
 return value > 0

puts positive?(10)
puts positive?(-3)


The method ends with return

The processing after return in the method is not executed.


def divide(a,b)
  return a/b
  puts "Divided"



Return value and if statement (pass the return value to the if statement)

Set a boolean value in the return value of the method and call the method with the conditional expression of the if statement.


#Method that returns a boolean value
def discount?(price)
 return price >= 1000


if discount?(price)
  puts "10%Discount. the price is#{price*0.9}is."
  puts "after#{1000-price}10 in yen%It is a discount"

10 for another 200 yen%It is a discount

Return value and if statement (use return and if statement in the method)

Use if statements and return values in methods.


#Discount consumption tax over 1000
def total_value(price)
 if price >= 1000
   return price
 return 1000*1.1


puts "The payment amount is#{total_value(800)}It's a yen"

The payment amount is 880 yen

Keyword arguments

Specify the value by the name of the argument. def method name (argument name A :, argument name B: ,,,) └ Add ":" after the argument name └ No change in processing

Method name (argument name B: value, argument name A: value ,,,) └ Match with the argument name defined in the method └ Add ":" after the argument name


def user(name:, age:, gender:, word:)
  puts "#{name}Is your age#{age}is"
  puts "what is your gender#{gender}is"
  puts "The habit is "#{word}"is"

user(gender:"male", name:"JoJo", age:"17", word:"Oraora Oraoraoraora")

JoJo is 17 years old"
Gender is male"
The habit is "Oraoraoraoraoraoraora""

Recommended Posts

[Ruby] Method definition summary
definition of ruby method
Ruby algorithm (inject, method definition)
Ruby to_s method
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
Ruby syntax summary
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
Method summary to update multiple columns [Ruby on Rails]
Ruby on Rails Refactoring method example summary around MVC
ruby qualification test point summary
Integer check method with ruby
Basic knowledge in method definition
[Ruby] Method that returns truth
Ruby on Rails validation summary
[ruby] Method call with argument
Ruby on Rails Overview (Beginner Summary)
Ruby design pattern template method pattern memo
Ruby on Rails variable, constant summary
[Ruby] present/blank method and postfix if.
Ruby environment construction summary ~ mac version ~
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
String output method memo in Ruby
[Swift] Protocol concept and definition method
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
[Ruby] Obtaining even values ​​using the even? Method
Ruby on Rails installation method [Mac edition]
[Ruby] From the basics to the inject method
Implemented "Floyd Cycle Detection Method" in Ruby
Summary of hashes and symbols in Ruby
Ruby print puts p printf output method
Ruby: Regular expression summary * Code sample available
ruby Exercise Memo II (variable and method)
Summary of CR, LF, CRL of open method
[Rails] Method summary for conversion / verification / search
[Java] Instance method, instance field, class method, class field, constructor summary
Extract characters from Ruby strings slice method