0
0

More than 1 year has passed since last update.

#Ruby キーワード引数はハッシュのままでメソッドに与えることが出来る - example : pass method keyword args with hash variable

Last updated at Posted at 2019-11-17

柔軟に受け取るようにできているっぽい

class A
  def initialize(arg1, arg2, a:, b:, c:)
    @arg1 = arg1
    @arg2 = arg2
    @keyword_arg_a = a
    @keyword_arg_b = b
    @keyword_arg_c = c
  end

  def echo
    p @arg1
    p @arg2
    p @keyword_arg_a
    p @keyword_arg_b
    p @keyword_arg_c
  end
end



A.new(:some, :how, a: 1, b: 2, c: 3).echo

options = { a: 1, b: 2, c: 3 }

A.new(:some, :how, **options).echo

A.new(:some, :how, options).echo

# :some
# :how
# 1
# 2
# 3

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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