class Euler::Problem

This class represents a project Euler problem.

Attributes

id[R]
name[R]
url[R]

Public Class Methods

find(id) click to toggle source

Returns the problem with the given id.

# 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
new(options) click to toggle source

Given a hash with symbol keys initialize the problem using the :id, :name, url, and :content keys.

# File lib/euler/problem.rb, line 24
def initialize options
  @id      = options[:id]
  @name    = options[:name]
  @url     = options[:url]
  @content = options[:content]
end

Public Instance Methods

answer() click to toggle source

Returns this problem's answer.

# File lib/euler/problem.rb, line 47
def answer
  @@answers ||= YAML.load_file(Euler.answers_file)
  @@answers[id].to_s
end
content() click to toggle source

Passing content though an ultra simple template engine before returning it

# File lib/euler/problem.rb, line 37
def content
  @content.gsub(/\{\{\s?images_dir\s?\}\}/, Euler.images_dir)
end
has_answer?() click to toggle source

Returns true if this problem has an answer.

# File lib/euler/problem.rb, line 42
def has_answer?
  not answer.empty?
end
template() click to toggle source

Content without going through the template engine

# File lib/euler/problem.rb, line 32
def template
  @content
end
to_hash() click to toggle source

Converts the problem to a hash.

# File lib/euler/problem.rb, line 53
def to_hash
  {
    id:       id,
    name:     name,
    url:      url,
    content:  template
  }
end
to_s() click to toggle source
# File lib/euler/problem.rb, line 67
def to_s
  "##{id} - #{name}"
end
to_yaml() click to toggle source

Converts the problem to YAML.

# File lib/euler/problem.rb, line 63
def to_yaml
  to_hash.to_yaml
end