Tintin81 Tintin81 - 1 year ago 74
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download