Class: Euler::Problem
- Inherits:
-
Object
- Object
- Euler::Problem
- Defined in:
- lib/euler/problem.rb
Overview
This class represents a project Euler problem.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.find(id) ⇒ Object
Returns the problem with the given id.
Instance Method Summary collapse
-
#answer ⇒ Object
Returns this problem’s answer.
-
#content ⇒ Object
Passing content though an ultra simple template engine before returning it.
-
#has_answer? ⇒ Boolean
Returns true if this problem has an answer.
-
#initialize(options) ⇒ Problem
constructor
Given a hash with symbol keys initialize the problem using the
:id
,:name
,url
, and:content
keys. -
#template ⇒ Object
Content without going through the template engine.
-
#to_hash ⇒ Object
Converts the problem to a hash.
- #to_s ⇒ Object
-
#to_yaml ⇒ Object
Converts the problem to YAML.
Constructor Details
#initialize(options) ⇒ Problem
Given a hash with symbol keys initialize the problem using the :id
, :name
, url
, and :content
keys.
24 25 26 27 28 29 |
# File 'lib/euler/problem.rb', line 24 def initialize @id = [:id] @name = [:name] @url = [:url] @content = [:content] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
20 21 22 |
# File 'lib/euler/problem.rb', line 20 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
20 21 22 |
# File 'lib/euler/problem.rb', line 20 def name @name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
20 21 22 |
# File 'lib/euler/problem.rb', line 20 def url @url end |
Class Method Details
.find(id) ⇒ Object
Returns the problem with the given id.
10 11 12 13 14 15 16 17 18 |
# File 'lib/euler/problem.rb', line 10 def self.find id problem_spec_file = "#{Euler.problems_dir}/#{id}.yml" begin problem_spec = YAML.load_file(problem_spec_file) rescue raise "No problem with id '#{id}' found" end Problem.new(problem_spec) end |
Instance Method Details
#answer ⇒ Object
Returns this problem’s answer.
47 48 49 50 |
# File 'lib/euler/problem.rb', line 47 def answer @@answers ||= YAML.load_file(Euler.answers_file) @@answers[id].to_s end |
#content ⇒ Object
Passing content though an ultra simple template engine before returning it
37 38 39 |
# File 'lib/euler/problem.rb', line 37 def content @content.gsub(/\{\{\s?images_dir\s?\}\}/, Euler.images_dir) end |
#has_answer? ⇒ Boolean
Returns true if this problem has an answer.
42 43 44 |
# File 'lib/euler/problem.rb', line 42 def has_answer? not answer.empty? end |
#template ⇒ Object
Content without going through the template engine
32 33 34 |
# File 'lib/euler/problem.rb', line 32 def template @content end |
#to_hash ⇒ Object
Converts the problem to a hash.
53 54 55 56 57 58 59 60 |
# File 'lib/euler/problem.rb', line 53 def to_hash { id: id, name: name, url: url, content: template } end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/euler/problem.rb', line 67 def to_s "##{id} - #{name}" end |
#to_yaml ⇒ Object
Converts the problem to YAML.
63 64 65 |
# File 'lib/euler/problem.rb', line 63 def to_yaml to_hash.to_yaml end |