Creating a struct from an array of symbols

Hello!

I’d like to create a Struct[1] from an array containing symbols representing the attributes of the Struct but can’t get it to work:

···

~~~
arr = [:attr1, :attr2, :attr3]

s = Struct.new(arr)
# => TypeError: no implicit conversion of Array into String
~~~

I’ve tried different conversions (to_s, etc.) and OpenStruct[2], too. Even though the latter is supposed to work dynamically it needs a hash and does not work with my array of symbols:

~~~
os = OpenStruct.new(arr)
# => NoMethodError: undefined method `each_pair’ for [:attr1, :attr2, :attr3]:Array

os = OpenStruct.new
os.arr[1]
# => NoMethodError: undefined method `[]' for nil:NilClass

~~~

Any ideas on how I can feed in my array of symbols into Struct.new or an OpenStruct?

Many thanks!

Cheers,
Michael

[1]: https://ruby-doc.org/core-2.4.0/Struct.html
[2]: https://ruby-doc.org/stdlib-2.4.0/libdoc/ostruct/rdoc/OpenStruct.html

I can't test it out from my phone but maybe try using the Splat operator?

arr = [:attr1, :attr2, :attr3]

s = Struct.new(*arr)

···

On Thu, Jan 12, 2017, 12:06 AM Michael Schwarze <michael@schwarze-web.de> wrote:

Hello!

I’d like to create a Struct[1] from an array containing symbols
representing the attributes of the Struct but can’t get it to work:

~~~
arr = [:attr1, :attr2, :attr3]

s = Struct.new(arr)
# => TypeError: no implicit conversion of Array into String
~~~

I’ve tried different conversions (to_s, etc.) and OpenStruct[2], too. Even
though the latter is supposed to work dynamically it needs a hash and does
not work with my array of symbols:

~~~
os = OpenStruct.new(arr)
# => NoMethodError: undefined method `each_pair’ for [:attr1, :attr2,
:attr3]:Array

os = OpenStruct.new
os.arr[1]
# => NoMethodError: undefined method `' for nil:NilClass

~~~

Any ideas on how I can feed in my array of symbols into Struct.new or an
OpenStruct?

Many thanks!

Cheers,
Michael

[1]: Class: Struct (Ruby 2.4.0)
[2]: Class: OpenStruct (Ruby 2.4.0)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--

- Raj

It does work:

2.2.1 :001 > a = [:a, :b, :c]
=> [:a, :b, :c]
2.2.1 :002 > A = Struct.new(*a)
=> A
2.2.1 :003 > A.members
=> [:a, :b, :c]

Jesus.

···

On Thu, Jan 12, 2017 at 9:40 AM, Raj Sahae <rajsahae@gmail.com> wrote:

I can't test it out from my phone but maybe try using the Splat operator?

arr = [:attr1, :attr2, :attr3]

s = Struct.new(*arr)

Many thanks, splat’s exactly what I was looking for!

···

Am 12.01.2017 um 09:43 schrieb Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Thu, Jan 12, 2017 at 9:40 AM, Raj Sahae <rajsahae@gmail.com> wrote:

I can't test it out from my phone but maybe try using the Splat operator?

arr = [:attr1, :attr2, :attr3]

s = Struct.new(*arr)

It does work:

2.2.1 :001 > a = [:a, :b, :c]
=> [:a, :b, :c]
2.2.1 :002 > A = Struct.new(*a)
=> A
2.2.1 :003 > A.members
=> [:a, :b, :c]

Jesus.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;