Tintin81 Tintin81 - 5 months ago 22
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



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