rubyでメソッドを定義する場合、各種パラメータの定義方法



#     
def add_values(a, b)
   a + b
end

#       
def add_values2(a = 1, b = 2, c)
   a + b + c
end

def add_values3(a = 1, b = a) #              
   a + b
end

def add_values4(a = b, b = 1) #           b   ,             
   a + b
end

p add_values4 # raise an error

#     
def my_method((a, b))
   p a: a, b: b
end

my_method([1, 2])

#       
def my_method2((a, *b))
   p a: a, b: b   # {:a=>1, :b=>2}
end

my_method2([1, 2, 3, 4]) # {:a=>1, :b=>[2, 3, 4]}

def my_method3((a, (b, c), d)) #            ,            ,        nil
   p a: a, b: b, c: c, d: d
end


my_method3(1)              # {:a=>1, :b=>nil, :c=>nil, :d=>nil}
my_method3([1, [1, 2], 4]) # {:a=>1, :b=>1, :c=>2, :d=>4}
my_method3([1, 2, 3])      # {:a=>1, :b=>2, :c=>nil, :d=>3}

def my_method4(((a, b), c))
   p a: a, b: b, c: c
end

my_method4([[1, 2], 3]) # {:a=>1, :b=>2, :c=>3}

def gather_arguments(*arg) #     ,                   
   p arg
end

gather_arguments(1, x: 20) # [1, {:x=>20}]                    ,                   。


def gather_arguments2(first_arg, *arg, last_arg)
   p [first_arg, arg, last_arg]
end

def gather_arguments_keyword(*positional, keyword: nil)
   p positional: positional, keyword: keyword
end
gather_arguments_keyword(1, 2, three: 3) # error unknown keyword: :three (ArgumentError) three       

def ignore_arguments(*) #       ,           
   p "none"
end

ignore_arguments(1, 2, 3) # none

#     
def add_values(first: 1, second: 2)
   p first + second
end

add_values(first: 3, second: 4) # 7

def gather_arguments3(first: nil, **rest)
   p first, rest
end

gather_arguments3(first: 1, second: 3, three: 4) # 1, {:second=>3, :three=>4} rest       


def add_values5(first:, second: ) #            
   p first + second
end

add_values5 # error missing keywords: :first, :second (ArgumentError)
add_values5(first: 1, second: 2) # 3

def add_values6(first, second: ) #                 
   p [first, second]
end

add_values6(1, second: 2) #[1, 2]

def ignore_keywords(**) #          
   ###
end

def no_keywords(**nil)
   ###
end

no_keywords(1, 2, 3) # error,          

def my_method5(**keywords)
   p keywords
end
my_method5(x: 1, y: 2) # {:x=>1, :y=>2}
my_method5({x: 3, y: 4}) #      {:x=>3, :y=>4}

def my_method6(hash = nil, **keywords)
   p [hash, keywords]
end

my_method6 10, x: 1, y: 2 # [10, {:x=>1, :y=>2}]

def my_method7(hash, **keywords)
   p [hash, keywords]
end
my_method7(1, x: 1, y: 2) # [1, {:x=>1, :y=>2}]

my_method7(x: 2) # [{:x=>2}, {}]                ,                 
my_method7({x: 2}) # [{:x=>2}, {}] same as above

my_method7({}) # [{}, {}]              

def my_method8(hash = 3, a: 4)
   p [hash, a]
end
my_method8(a: 1, 'a' => 2)       #      ,              hash,              ,  a  1,[{"a"=>2}, 1]
my_method8({a: 1, 'a' => 2}) # [{"a"=>2}, 1] #   

#    
def my_method9(&block)
   block.call(self)
end
my_method9 { |var| p var } # main


def my_method10(&block)
   return [1, 2, 3].each(&block) #        each  
end

my_method10 { |var| p var } # 1 2 3

def my_method11 #      
   yield self
end
my_method11 { |var| p var } # main

#         
def my_method12
   begin
      raise "bad"
   rescue
      puts "bad thing happend"
   end
end

my_method12 # bad thing happend

def my_method13
   raise "good"
rescue
   puts "good thing happened"
end
my_method13 # good thing happened      

def my_method14
   raise "again"
ensure
   puts "again happend"
end

my_method14 #    ensure   ,       

def my_method15
   raise "again again"
rescue
   puts "again again happend"
else                    #        
   puts "no exception"
ensure
   puts "always execute!"
end
my_method15

def my_method16
   puts "again again"
rescue
   puts "again again happend"
else                       #        
   puts "no exception"
ensure                     #           
   puts "always execute!"
end