Class: Euler::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/euler/problem.rb

Overview

This class represents a project Euler problem.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 options
  @id      = options[:id]
  @name    = options[:name]
  @url     = options[:url]
  @content = options[:content]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/euler/problem.rb', line 16

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/euler/problem.rb', line 16

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url.



16
17
18
# File 'lib/euler/problem.rb', line 16

def url
  @url
end

Class Method Details

.find(id) ⇒ Object

Returns the problem with the given id.



10
11
12
13
14
# File 'lib/euler/problem.rb', line 10

def self.find id
  problem_spec_file = "#{Euler.problems_dir}/#{id}.yml"
  problem_spec      = YAML.load_file(problem_spec_file)
  Problem.new problem_spec
end

Instance Method Details

#answerObject

Returns this problem’s answer.



38
39
40
41
# File 'lib/euler/problem.rb', line 38

def answer
  @@answers ||= YAML.load_file(Euler.answers_file)
  @@answers[id].to_s
end

#contentObject

Passing content though an ultra simple template engine before returning it



28
29
30
# File 'lib/euler/problem.rb', line 28

def content
  @content.gsub(/\{\{\s?images_dir\s?\}\}/, Euler.images_dir)
end

#has_answer?Boolean

Returns true if this problem has an answer.

Returns:



33
34
35
# File 'lib/euler/problem.rb', line 33

def has_answer?
  not answer.empty?
end

#to_hashObject

Converts the problem to a hash.



44
45
46
47
48
49
50
51
# File 'lib/euler/problem.rb', line 44

def to_hash
  {
    id:       id,
    name:     name,
    url:      url,
    content:  content
  }
end

#to_sObject



58
59
60
# File 'lib/euler/problem.rb', line 58

def to_s
  "##{id} - #{name}"
end

#to_yamlObject

Converts the problem to YAML.



54
55
56
# File 'lib/euler/problem.rb', line 54

def to_yaml
  to_hash.to_yaml
end