Class: PopCap::TagStruct
- Inherits:
-
Object
- Object
- PopCap::TagStruct
- Includes:
- Enumerable
- Defined in:
- lib/pop_cap/tag_struct.rb
Overview
Public: This class is a simple implementation of a Ruby struct like object. It has a custom #to_s method & creates getter methods.
new - This method takes a hash. It raises an error if anything else is provided.
Examples
ts = TagStruct.new({artist: 'Artist', date: 1984})
ts.artist => 'Artist'
ts.date => 1984
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Public: This method is a custom each iterator for tags.
-
#initialize(hash) ⇒ TagStruct
constructor
A new instance of TagStruct.
-
#to_s ⇒ Object
Public: This method shows the class name & hash values as a string.
Constructor Details
#initialize(hash) ⇒ TagStruct
Returns a new instance of TagStruct.
16 17 18 19 20 |
# File 'lib/pop_cap/tag_struct.rb', line 16 def initialize(hash) raise(ArgumentError, ) unless hash.kind_of?(Hash) @hash = hash define_instance_methods end |
Instance Method Details
#each(&block) ⇒ Object
Public: This method is a custom each iterator for tags.
36 37 38 39 40 |
# File 'lib/pop_cap/tag_struct.rb', line 36 def each(&block) @hash.each do |elem| block_given? ? block.call(elem) : elem end end |
#to_s ⇒ Object
29 30 31 32 |
# File 'lib/pop_cap/tag_struct.rb', line 29 def to_s methods = @hash.map { |key,val| %(#{key}: #{val}) }.join(', ') "#<#{self.class.name} " + methods + '>' end |