Class: EvoSynth::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/evosynth/core/profile.rb

Overview

This class is used to create and maintain a evolver profile. Basically a Profile can be seen as a dynamic Hash, where values can be added and removed at will.

Instance Method Summary collapse

Constructor Details

#initialize(*properties) ⇒ Profile

Creates a new Profile using a given hash of symbols and values.

usage:

profile = EvoSynth::Profile.new(
    :individual      => MaxOnes.create_individual,
    :population      => EvoSynth::Population.new(POP_SIZE) { MaxOnes.create_individual },
    :evaluator       => MaxOnes::MaxOnesEvaluator.new,
    :mutation        => EvoSynth::Mutations::BinaryMutation.new(EvoSynth::Mutations::Functions::FLIP_BOOLEAN)
)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/evosynth/core/profile.rb', line 43

def initialize(*properties)
	@properties = {}

	properties.each do |property|
		if property.is_a?(Symbol)
			add_symbol(property, nil)
		elsif property.is_a?(Hash)
			add_hash(property)
		else
			raise ArgumentError, "argument type not supported"
		end
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Used to dynamically add key/value pairs.

p = EvoSynth::Profile.new
p.foo                        #=> raises ArgumentError
p.foo = "bar"                #=> adds key 'foo' to p and sets its value to 'bar'
p.foo                        #=> 'bar'


64
65
66
67
68
69
70
71
72
# File 'lib/evosynth/core/profile.rb', line 64

def method_missing(method_name, *args)
	if method_name[-1] == "="
		args = args[0] if args.size == 1
		add_symbol(method_name[0..method_name.size-2].to_sym, args)
	else
		raise ArgumentError.new("Profile does not contain a value for '#{method_name}'.") unless @properties.has_key?(method_name)
		@properties[method_name]
	end
end

Instance Method Details

#delete(key) ⇒ Object

:call-seq: delete(key) -> nil or key

Removes a given key from the profile. Returns the key if it was member of the profile, nil otherwise.

p = EvoSynth::Profile.new
p.foo = "bar"                    #=> adds key 'foo' to p and sets its value to 'bar'
p.delete(:foo)                   #=> 'bar'
p.foo                            #=> raises ArgumentError


84
85
86
# File 'lib/evosynth/core/profile.rb', line 84

def delete(key)
	@properties.delete(key)
end

#to_sObject

Return a printable version of the profile.



90
91
92
# File 'lib/evosynth/core/profile.rb', line 90

def to_s
	"evolver profile <#{@properties.to_s}>"
end