Tintin81 Tintin81 - 7 months ago 33
Ruby Question

How to DRY up Ruby initialize method?

In my Rails application I have this class:

class Plan

attr_reader :name, :id, :amount, :interval, :maximum, :features

def initialize(id, name, amount, interval, maximum, features)
@id = id
@name = name
@amount = amount
@interval = interval
@maximum = maximum
@features = features
end

...

end


Is there a way to DRY up this class?

Answer Source

You can do it in one line:

def initialize(*args)
  @id, @name, @amount, @interval, @maximum, @features = args
end