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
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#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.
-
#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:contentkeys. -
#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.
20 21 22 23 24 25 |
# File 'lib/euler/problem.rb', line 20 def initialize @id = [:id] @name = [:name] @url = [:url] @content = [:content] end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
16 17 18 |
# File 'lib/euler/problem.rb', line 16 def content @content end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
16 17 18 |
# File 'lib/euler/problem.rb', line 16 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/euler/problem.rb', line 16 def name @name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
16 17 18 |
# File 'lib/euler/problem.rb', line 16 def url @url end |
Class Method Details
Instance Method Details
#answer ⇒ Object
Returns this problem’s answer.
33 34 35 36 |
# File 'lib/euler/problem.rb', line 33 def answer @@answers ||= YAML.load_file(Euler.answers_file) @@answers[id].to_s end |
#has_answer? ⇒ Boolean
Returns true if this problem has an answer.
28 29 30 |
# File 'lib/euler/problem.rb', line 28 def has_answer? not answer.empty? end |
#to_hash ⇒ Object
Converts the problem to a hash.
39 40 41 42 43 44 45 46 |
# File 'lib/euler/problem.rb', line 39 def to_hash { id: id, name: name, url: url, content: content } end |
#to_s ⇒ Object
53 54 55 |
# File 'lib/euler/problem.rb', line 53 def to_s "##{id} - #{name}" end |
#to_yaml ⇒ Object
Converts the problem to YAML.
49 50 51 |
# File 'lib/euler/problem.rb', line 49 def to_yaml to_hash.to_yaml end |