# Types.Constructor failing on Ruby 3.1

**URL:** https://discourse.dry-rb.org/t/types-constructor-failing-on-ruby-3-1/1566
**Category:** dry-types
**Created:** [December 6, 2022, 5:28pm UTC](https://discourse.dry-rb.org/t/types-constructor-failing-on-ruby-3-1/1566 "2022-12-06T17:28:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eaylward8](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/eaylward8/32/824_2.png) [@eaylward8](https://discourse.dry-rb.org/u/eaylward8)
#### Post date: [December 6, 2022, 5:28pm UTC](https://discourse.dry-rb.org/t/types-constructor-failing-on-ruby-3-1/1566/1 "2022-12-06T17:28:29Z")

</div>

I saw Ruby 3.1 issue threads for some other dry gems, but not dry-types. Apologies if I missed this somewhere and am duplicating things.

I was trying to follow along with [this example](https://dry-rb.org/gems/dry-types/1.2/custom-types/#code-types-constructor-code) and hit an error.

Given this class:

```ruby
class User
  def initialize(name:)
    @name = name
  end
end

```

…the docs say I should be able to do this:

```ruby
user_type = Types.Constructor(User)

# It is equivalent to User.new(name: 'John')
user_type[name: 'John']

```

On Ruby 2.7.7, this works as expected and I get an instance of User with the given name.

On Ruby 3.1.2, I get:

```ruby
Dry::Types::CoercionError: wrong number of arguments (given 1, expected 0; required keyword: name)
Caused by ArgumentError: wrong number of arguments (given 1, expected 0; required keyword: name)
from (pry):2:in `initialize'

```

I’m assuming this is due to Ruby 3.1’s changes to how keyword args are handled.

---

<div class="post-metadata">

### Author: ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)
#### Post date: [December 8, 2022, 5:09am UTC](https://discourse.dry-rb.org/t/types-constructor-failing-on-ruby-3-1/1566/2 "2022-12-08T05:09:38Z")

</div>

Confirmed, I see this behavior also. I’m not certain that this actually falls within Dry::Types’s use-case; if you wanted attribute-based types you could use Dry::Struct for that.

I would suggest opening a support ticket on GitHub. In the meantime, you can work around it like this:

```ruby
user_type = Types.Constructor(User) { |input| User.new(**input) }

```

---

<div class="post-metadata">

### Author: ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)
#### Post date: [December 9, 2022, 8:00am UTC](https://discourse.dry-rb.org/t/types-constructor-failing-on-ruby-3-1/1566/3 "2022-12-09T08:00:23Z")

</div>

It’s not a compatible `User` declaration. dry-types doesn’t pass value as keywords. It should be

```auto
class User
  def initialize(attributes)
    attributes.each { instance_variable_set(:"@{_1}", _2) }
  end
end

```

Otherwise, you can use a custom block as @alassek suggested
