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)
a + b
end
p add_values4
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
end
my_method2([1, 2, 3, 4])
def my_method3((a, (b, c), d))
p a: a, b: b, c: c, d: d
end
my_method3(1)
my_method3([1, [1, 2], 4])
my_method3([1, 2, 3])
def my_method4(((a, b), c))
p a: a, b: b, c: c
end
my_method4([[1, 2], 3])
def gather_arguments(*arg)
p arg
end
gather_arguments(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)
def ignore_arguments(*)
p "none"
end
ignore_arguments(1, 2, 3)
def add_values(first: 1, second: 2)
p first + second
end
add_values(first: 3, second: 4)
def gather_arguments3(first: nil, **rest)
p first, rest
end
gather_arguments3(first: 1, second: 3, three: 4)
def add_values5(first:, second: )
p first + second
end
add_values5
add_values5(first: 1, second: 2)
def add_values6(first, second: )
p [first, second]
end
add_values6(1, second: 2)
def ignore_keywords(**)
end
def no_keywords(**nil)
end
no_keywords(1, 2, 3)
def my_method5(**keywords)
p keywords
end
my_method5(x: 1, y: 2)
my_method5({x: 3, y: 4})
def my_method6(hash = nil, **keywords)
p [hash, keywords]
end
my_method6 10, x: 1, y: 2
def my_method7(hash, **keywords)
p [hash, keywords]
end
my_method7(1, x: 1, y: 2)
my_method7(x: 2)
my_method7({x: 2})
my_method7({})
def my_method8(hash = 3, a: 4)
p [hash, a]
end
my_method8(a: 1, 'a' => 2)
my_method8({a: 1, 'a' => 2})
def my_method9(&block)
block.call(self)
end
my_method9 { |var| p var }
def my_method10(&block)
return [1, 2, 3].each(&block)
end
my_method10 { |var| p var }
def my_method11
yield self
end
my_method11 { |var| p var }
def my_method12
begin
raise "bad"
rescue
puts "bad thing happend"
end
end
my_method12
def my_method13
raise "good"
rescue
puts "good thing happened"
end
my_method13
def my_method14
raise "again"
ensure
puts "again happend"
end
my_method14
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