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.

#Ruby 2.7 – Try code example – receive args and keyword args both with asterisk ( single splat and double splat both )

Last updated at Posted at 2020-05-01
# Ruby 2.7
# receive args and keyword args both with asterisk

def foo(*args, **kwargs)
  p args
  p kwargs
end

# Single arg
foo("YES")
# ["YES"]
# {}

# Multiple args
foo("AH", "WOW")
# ["AH", "WOW"]
# {}

# Array single args
foo(["WOW", "YEAH"])
# [["WOW", "YEAH"]]
# {}

# Array multiple args
foo(["SAY", "YES"], ["SAY", "NO"])
# [["SAY", "YES"], ["SAY", "NO"]]
# {}


# Args + Keyword Args
foo("SOME", "HOW", x: "X", y: "y")
# ["SOME", "HOW"]
# {:x=>"X", :y=>"y"}

# Only Keyword Args
foo(x: "X", y: "y")
# []
# {:x=>"X", :y=>"y"}

# Hash args
# warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
# *args will be empty!
# *kwargs takes hash!
foo({ x: "X", y: "y" })
# []
# {:x=>"X", :y=>"y"}

# Multiple args + Hash
# warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
foo("BIG", "CHANCE", { new: :age, will: :come })
# ["BIG", "CHANCE"]
# {:new=>:age, :will=>:come}

# Unable to use arg after keyword arg
# Syntax Error
# syntax error, unexpected ')'
#
# foo(x: "X", y: "y", "A")

Original by Github issue

チャットメンバー募集

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

Twitter

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?