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 ( Rails ) ActiveInteraction receives keyword args or hash ?

Last updated at Posted at 2020-05-02

ActiveInteraction seems various type args

require "active_interaction"

class ServiceClass < ActiveInteraction::Base
  string :x
  string :y

  def execute
    p x
    p y
  end
end

# OK
# Keyword args
ServiceClass.run!(x: "X1", y: "Y1")

# OK
# Convert Hash to keyword args
ServiceClass.run!(**{ x: "X2", y: "Y2" })

# OK
# Hash args
# No Warning happens
ServiceClass.run!({ x: "X3", y: "Y3" })

# OK
# Not Symbol string key
ServiceClass.run!({ "x" => "X4", "y" => "Y4" })

# OK
# Not symbol string key
# Convert Hash to keyword args
ServiceClass.run!(**{ "x" => "X5", "y" => "Y5" })


class Foo
  def initialize(x: , y:)
    @x = x
    @y = y
  end

  def run!
    p @x
    p @y
  end
end

# OK
# Keyword args
Foo.new(x: "XX1", y: "YY1").run!

# OK
# Convert Hash to keyword args
Foo.new(**{ x: "XX2", y: "YY2" }).run!

# OK
# Hash args
# warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
Foo.new({ x: "XX3", y: "YY3" }).run!

# NG
# Not Symbol string key
# ArgumentError
Foo.new({ "x" => "XX4", "y" => "YY4" }).run!

# OK
# Not symbol string key
# ArgumentError
Foo.new(**{ "x" => "XX5", "y" => "YY5" }).run!

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?