DRY'er Object Initialization in Ruby
Posted
by Trevoro
on Stack Overflow
See other posts from Stack Overflow
or by Trevoro
Published on 2009-06-11T18:34:38Z
Indexed on
2010/06/11
6:52 UTC
Read the original article
Hit count: 175
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
© Stack Overflow or respective owner