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.

はじめに

移植やってます。
( from python 3.7 to ruby 2.7 )

仮引数 (Python)

def func(a, b=1, c=2, **kwargs):
    print(a, b, c)

func(0, 4, 5)
func(0, d=4, c=5)

# 0 4 5
# 0 1 5

d=4が実引数の2番目にありますが、辞書扱いとなり仮引数の2番目のbには格納されないようです。

独習Python 330p

仮引数 失敗 (Ruby)

def func(a, b=1, c=2, **kwargs)
  p [a, b, c]
end

func(0, 4, 5)
func(0, d=4, c=5)

# [0, 4, 5]
# [0, 4, 5]

d=4の戻り値である4bに格納されます。

仮引数 成功 (Ruby)

def func(a, b: 1, c: 2, **kwargs)
  p [a, b, c]
end

func(0, b: 4, c: 5)
func(0, d: 4, c: 5)

# [0, 4, 5]
# [0, 1, 5]

独習Ruby 387p

プロを目指す人のためのRuby入門[改訂2版] 186p

仮引数 成功2 (Ruby)

def func(a, b: 1, c: 2, **kwargs)
  p [a, b, c]
end

func(0, b: 4, c: 5)
func(0, 'd' => 4, c: 5)

# [0, 4, 5]
# [0, 1, 5]

このほうが紛らわしくないかも。

メモ

  • Python の 仮引数の既定値 を学習した
  • 百里を行く者は九十里を半ばとす
0
0
0

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?