class Euler::Solution

This class represents a user created solution to a project Euler problem.

Attributes

language[R]

Public Class Methods

all() click to toggle source

Returns an array of all of the solutions

# File lib/euler/solution.rb, line 10
def self.all
  Euler.all_solutions_strategy
end
new(problem, language) click to toggle source

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

answer() click to toggle source

Returns this solution's answer.

# File lib/euler/solution.rb, line 52
def answer
  problem.answer
end
correct?() click to toggle source

Alias for test.

# File lib/euler/solution.rb, line 74
def correct?
  test
end
dir() click to toggle source

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
init() click to toggle source

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
problem() click to toggle source

Returns the problem this solution is for.

# File lib/euler/solution.rb, line 28
def problem
  @problem ||= Problem.find(@problem_id)
end
problem_id() click to toggle source

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
result() click to toggle source

Alias for run.

# File lib/euler/solution.rb, line 62
def result
  run
end
run() click to toggle source

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
test() click to toggle source

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

language_object() click to toggle source

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
mkdir() click to toggle source

Used to create this solution's directory.

# File lib/euler/solution.rb, line 87
def mkdir
  Euler.create_directory_strategy(self)
end