DRY'er Object Initialization in Ruby
- by Trevoro
Hi,
Is there a more 'DRY' way to do the following in ruby?
#!/usr/bin/env ruby
class Volume
attr_accessor :name, :size, :type, :owner, :date_created, :date_modified, :iscsi_target, :iscsi_portal
SYSTEM = 0
DATA = 1
def initialize(args={:type => SYSTEM})
@name = args[:name]
@size = args[:size]
@type = args[:type]
@owner = args[:owner]
@iscsi_target = args[:iscsi_target]
@iscsi_portal = args[:iscsi_portal]
end
def inspect
return {:name => @name,
:size => @size,
:type => @type,
:owner => @owner,
:date_created => @date_created,
:date_modified => @date_modified,
:iscsi_target => @iscsi_target,
:iscsi_portal => @iscsi_portal }
end
def to_json
self.inspect.to_json
end
end