0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python to RubyAdvent Calendar 2021

Day 24

[py2rb] 引数の省略

Last updated at Posted at 2022-01-04

はじめに

移植やってます。

引数の省略 (Python)

def func1(str1, num1=(1, 2), num2=0):
    print(num1[1] + num2)

func1('test', num2=5)

# 7

別段変わった様子はなし。

引数の省略 (Ruby)

def func1(str1, num1=[1, 2], num2=0)
  puts(num1[1] + num2)
end

func1('test', num2=5)

# 0

うぅ、これに手間取りました。

この時のnum1num2には、それぞれ[1, 0]0が入っています。
よって0が返ってくるのは当然ですね。
しかし5はどこにいった?

キーワード引数 (Ruby)

def func1(str1, num1: [1, 2], num2: 0)
  print(num1[1] + num2)
end

func1('test', num2: 5)

キーワード引数は~~Ruby 2.7~~Ruby 2.0辺りから入ったらしいです。
皆さんも利用しましょう。

メモ

  • Python の 引数の省略 を学習した
  • 道のりは遠そう
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?