How can I override Array Constructor in Ruby?

Crixx93

I have the following class:

class Library < Array
  attr_accessor :authors
end

And I would like to initilize the authors attribute inside the constructor of the class, and still use the Array#new behaviour. I tried this:

class Library < Array
  attr_accessor :authors

  def initialize(**args)
     @authors = {key1: 'val1', key2: 'val2'}
     super(args)
  end

end

But that yields an error.

How can I achieve this ?

Jack Noble

So the #initialize override is failing because you're using object splat (**) instead of array splat (*).

But you shouldn't do this. Subclassing Ruby's core classes will lead to all sorts of counter-intuitive behavior because they have many methods that create new instances of that class -- that won't be updated to create new instances of your class. For example, Library.new.reverse and Library.new + Library.new will both return new arrays, not libraries.

Instead you can get the behavior you want by creating an array instance variable in your class and delegating to it. You can define #each and get all the Ruby enumerable methods. You can also define any array methods you want.

class Library
  include Enumerable
  attr_accessor :authors

  def initialize(*args)
    @authors = {key1: 'val1', key2: 'val2'}
    @array = args
  end

  def each(&block)
    @array.each(&block)
  end

  def [](index)
    @array[index]
  end
end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I create an array from a method with ruby

分類Dev

How can I override set method in class to work with array.push?

分類Dev

How can I override another .bbappend

分類Dev

How can I override log level?

分類Dev

How do I create a constructor for a custom array class that can take in any amount of arguments up until the size of the array?

分類Dev

Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

分類Dev

How can I override settings in subprojects in an SBT project with multiple subprojects

分類Dev

How can I override the Actions buttons of Material-Table of react

分類Dev

How can I prevent cache override expiry time in redis

分類Dev

How can i read lines in a textfile with RUBY

分類Dev

How can I upgrade the Ruby version on Passenger?

分類Dev

How to override a base class constructor in Javascript

分類Dev

How can I destructure this array?

分類Dev

PHP : How can I check Array in array?

分類Dev

How can I pass my class into its own constructor?

分類Dev

how can I use JPopupMenu actionlistener inside a class constructor

分類Dev

How can I access a class property in an arrow function passed to a constructor?

分類Dev

How can i create a constructor with generics with same class as argument

分類Dev

How can I set a constructor object value to be a function of other values?

分類Dev

How can I initialize "array a" that contains "array b" that contains "array a"

分類Dev

How do I format an array of objects by iteration in Model in Ruby on Rails

分類Dev

How can I modify array fields in place?

分類Dev

How can I get array of UIStoryboard in Swift?

分類Dev

How can I concatenate an array in jade?

分類Dev

How can I push data into an array?

分類Dev

How Can I replace "NaN" with a space in an array

分類Dev

How can I sort a multidimensional array naturally?

分類Dev

how can i trasfere data object to an array

分類Dev

How can I convert an async iterator to an array?

Related 関連記事

  1. 1

    How can I create an array from a method with ruby

  2. 2

    How can I override set method in class to work with array.push?

  3. 3

    How can I override another .bbappend

  4. 4

    How can I override log level?

  5. 5

    How do I create a constructor for a custom array class that can take in any amount of arguments up until the size of the array?

  6. 6

    Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

  7. 7

    How can I override settings in subprojects in an SBT project with multiple subprojects

  8. 8

    How can I override the Actions buttons of Material-Table of react

  9. 9

    How can I prevent cache override expiry time in redis

  10. 10

    How can i read lines in a textfile with RUBY

  11. 11

    How can I upgrade the Ruby version on Passenger?

  12. 12

    How to override a base class constructor in Javascript

  13. 13

    How can I destructure this array?

  14. 14

    PHP : How can I check Array in array?

  15. 15

    How can I pass my class into its own constructor?

  16. 16

    how can I use JPopupMenu actionlistener inside a class constructor

  17. 17

    How can I access a class property in an arrow function passed to a constructor?

  18. 18

    How can i create a constructor with generics with same class as argument

  19. 19

    How can I set a constructor object value to be a function of other values?

  20. 20

    How can I initialize "array a" that contains "array b" that contains "array a"

  21. 21

    How do I format an array of objects by iteration in Model in Ruby on Rails

  22. 22

    How can I modify array fields in place?

  23. 23

    How can I get array of UIStoryboard in Swift?

  24. 24

    How can I concatenate an array in jade?

  25. 25

    How can I push data into an array?

  26. 26

    How Can I replace "NaN" with a space in an array

  27. 27

    How can I sort a multidimensional array naturally?

  28. 28

    how can i trasfere data object to an array

  29. 29

    How can I convert an async iterator to an array?

ホットタグ

アーカイブ