jeudi 21 mai 2020

Can array elements be attribute readers in ruby?

I have a class with one of the input parameters to the constructor an array:

class MyClass
  def initialize(types: types)
    @types = types  #types in an array
  end
  attr_reader :types

  def some_func
   types.each do { |type| do_something(type) }
  end

  def do_something(type)
    call1(type)
    call2(type)
    call3(type)
  end
end

Basically, I want to avoid using type as a function argument since it is being used for so many function calls. is there a way I can make it an attribute and avoid having to use it as a argument to so many function calls?

Thanks!

Aucun commentaire:

Enregistrer un commentaire