Class: Euler::Solution
- Inherits:
-
Object
- Object
- Euler::Solution
- Defined in:
- lib/euler/solution.rb
Overview
This class represents a user created solution to a project Euler problem.
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
Class Method Summary collapse
-
.all ⇒ Object
Returns an array of all of the solutions.
Instance Method Summary collapse
-
#answer ⇒ Object
Returns this solution’s answer.
-
#correct? ⇒ Boolean
Alias for
test
. -
#dir ⇒ Object
Returns the directory assigned to this solution by calling
Euler.directory_strategy
. -
#init ⇒ Object
Initialize this solution.
-
#initialize(problem, language) ⇒ Solution
constructor
Given the problem this solution is for an the language it’s implemented in initialize the instance.
-
#problem ⇒ Object
Returns the problem this solution is for.
-
#problem_id ⇒ Object
Returns the id of the problem this solution is for.
-
#result ⇒ Object
Alias for
run
. -
#run ⇒ Object
Returns the result of running this solution.
-
#test ⇒ Object
Returns true if this solution is correct.
Constructor Details
#initialize(problem, language) ⇒ Solution
Given the problem this solution is for an the language it’s implemented in initialize the instance.
18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
14 15 16 |
# File 'lib/euler/solution.rb', line 14 def language @language end |
Class Method Details
.all ⇒ Object
Returns an array of all of the solutions
10 11 12 |
# File 'lib/euler/solution.rb', line 10 def self.all Euler.all_solutions_strategy end |
Instance Method Details
#answer ⇒ Object
Returns this solution’s answer.
52 53 54 |
# File 'lib/euler/solution.rb', line 52 def answer problem.answer end |
#correct? ⇒ Boolean
Alias for test
.
74 75 76 |
# File 'lib/euler/solution.rb', line 74 def correct? test end |
#dir ⇒ Object
Returns the directory assigned to this solution by calling Euler.directory_strategy
.
80 81 82 |
# File 'lib/euler/solution.rb', line 80 def dir Euler.directory_strategy(self) end |
#init ⇒ Object
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.
43 44 45 46 47 48 49 |
# File 'lib/euler/solution.rb', line 43 def init mkdir if language_object.respond_to?(:init) language_object.init(self) end self end |
#problem ⇒ Object
Returns the problem this solution is for.
28 29 30 |
# File 'lib/euler/solution.rb', line 28 def problem @problem ||= Problem.find(@problem_id) end |
#problem_id ⇒ Object
Returns the id of the problem this solution is for.
33 34 35 |
# File 'lib/euler/solution.rb', line 33 def problem_id @problem_id ||= @problem.id end |
#result ⇒ Object
Alias for run
.
62 63 64 |
# File 'lib/euler/solution.rb', line 62 def result run end |
#run ⇒ Object
Returns the result of running this solution.
57 58 59 |
# File 'lib/euler/solution.rb', line 57 def run @result ||= (language_object.run(self) || '').gsub(/\r?\n/, '') end |
#test ⇒ Object
Returns true if this solution is correct.
67 68 69 70 71 |
# File 'lib/euler/solution.rb', line 67 def test expected = answer result = run expected == Digest::SHA1.hexdigest(result) end |