[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

python


def method name
processing
end

└ end required

Method call

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

Method example


def hello
 puts "Hello"
end

hello

#output
Hello

Pass arguments

python


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

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

python


def method name
return value
end

Return value example


def divide(a,b)
  return a/b
end

puts add(10,5)

value=add(10,5)
puts "The division result is#{value}is"

#output
2
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
end

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

#output
true
false

The method ends with return

The processing after return in the method is not executed.

python


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

divide(10,5)

#output
2

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.

python


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


price=800

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


#output
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.

python


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

end

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

#output
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

python


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"
end

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

#output
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
Method
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
Summary
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