class Euler::Solution
This class represents a user created solution to a project Euler
problem.
Attributes
Public Class Methods
Returns an array of all of the solutions
# File lib/euler/solution.rb, line 10 def self.all Euler.all_solutions_strategy end
Given the problem this solution is for an the language it's implemented in initialize the instance.
# File lib/euler/solution.rb, line 18 def initialize problem, language if problem.is_a?(Problem) @problem = problem else @problem_id = problem end @language = language end
Public Instance Methods
Returns this solution's answer.
# File lib/euler/solution.rb, line 52 def answer problem.answer end
Alias for test
.
# File lib/euler/solution.rb, line 74 def correct? test end
Returns the directory assigned to this solution by calling Euler.directory_strategy
.
# File lib/euler/solution.rb, line 80 def dir Euler.directory_strategy(self) end
Initialize this solution. This means:
-
run the
create_directory_strategy
to initialize the solutions
directory.
-
Run this solution's language's init method to do any extra
initialization steps required by the language.
# File lib/euler/solution.rb, line 43 def init mkdir if language_object.respond_to?(:init) language_object.init(self) end self end
Returns the problem this solution is for.
# File lib/euler/solution.rb, line 28 def problem @problem ||= Problem.find(@problem_id) end
Returns the id of the problem this solution is for.
# File lib/euler/solution.rb, line 33 def problem_id @problem_id ||= @problem.id end
Alias for run
.
# File lib/euler/solution.rb, line 62 def result run end
Returns the result of running this solution.
# File lib/euler/solution.rb, line 57 def run @result ||= (language_object.run(self) || '').gsub(/\r?\n/, '') end
Returns true if this solution is correct.
# File lib/euler/solution.rb, line 67 def test expected = answer result = run expected == Digest::SHA1.hexdigest(result) end
Protected Instance Methods
Returns the object instance of the class which represents this solution's language. Not to be confused with language
which just returns the name of the solutions language.
# File lib/euler/solution.rb, line 94 def language_object @language_object ||= Euler.get_language(language) end
Used to create this solution's directory.
# File lib/euler/solution.rb, line 87 def mkdir Euler.create_directory_strategy(self) end