Rubeuler 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
1
  0.0.1 - Initial release, allows you to initialize an instance of Rubeuler::Problem with required options `:number` (int) and `:answer` (string of ruby code). Answer is evaluated and compared to answer predefined in Rubeuler::Problem#solutions method. Result is returned indicating success/failure and the time elapsed when running the code in `:answer`
2
+
2
3
  0.0.2 - Fixed Rakefile (gave myself the ability to use `rake release`); Updated description in gemspec
4
+
3
5
  0.0.3 - Moved Rubeuler::Problem#solutions to Rubeuler::Solution#for_problem to make the intended use more intuitive
6
+
7
+ 0.0.4 - Add tests using minitest
8
+
9
+ 0.0.5 - Feature: Track other variables... the options `tracked: {foo: @foo}` would track a variable set as `@foo` in Rubeuler::Problem's `:answer` argument
10
+ Improve on tests, improve timer by moving Benchmark closer to code being evaluated,
11
+
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Rubeuler
1
+ Rubeuler (sounds like rube-oiler)
2
2
  ========
3
3
 
4
4
  For Rubyists who enjoy solving and optimizing [Project Euler](http://www.projecteuler.net) problems. It uses `Benchmark` so be sure to use this with Ruby >= 1.9.3
data/lib/rubeuler/base.rb CHANGED
@@ -6,6 +6,8 @@ module Rubeuler
6
6
 
7
7
  def load_options(*option_names, options)
8
8
  option_names.each{|o| load_option(o, options) }
9
+
10
+ option_names << instance_variable_set("@tracked", options[:tracked]) if options[:tracked]
9
11
  end
10
12
  end
11
13
  end
@@ -7,22 +7,33 @@ module Rubeuler
7
7
  def initialize(options)
8
8
  load_options(:number, :answer, options)
9
9
  raise TypeError, ':answer should be a string' unless @answer.is_a?(String)
10
+ raise TypeError, ':number should be an integer' unless @number.is_a?(Fixnum)
10
11
  end
11
12
 
12
13
  def execute!
13
- time = Benchmark.measure { @data = answer }.real
14
+ time = timed_answer
14
15
  true_or_false = @data == solution ? true : false
15
16
 
16
- return Rubeuler::Result.new(success: true_or_false, problem: @number, data: @data, runtime: time)
17
+ return Rubeuler::Result.new(success: true_or_false, problem: @number, data: data, runtime: time)
17
18
  end
18
19
 
19
20
  private
20
- def answer
21
- instance_eval(@answer.gsub("\n",";"))
21
+ def timed_answer
22
+ timer_endpoints = ["Benchmark.measure { @data =", "}.real"]
23
+ instance_eval(timer_endpoints.join(@answer.gsub("\n",";")))
22
24
  end
23
25
 
24
26
  def solution
25
27
  Rubeuler::Solution.for_problem(@number)
26
28
  end
29
+
30
+ def data
31
+ result_data = {
32
+ solution: @data
33
+ }
34
+ @tracked.each { |k,v| result_data.merge!({ k => instance_eval(v.to_s) }) } if @tracked
35
+
36
+ result_data
37
+ end
27
38
  end
28
39
  end
@@ -5,5 +5,17 @@ module Rubeuler
5
5
  @data = options[:data]
6
6
  @runtime = options[:runtime]
7
7
  end
8
+
9
+ def data
10
+ @data
11
+ end
12
+
13
+ def runtime
14
+ @runtime
15
+ end
16
+
17
+ def successful?
18
+ !!@success
19
+ end
8
20
  end
9
21
  end
@@ -9,7 +9,7 @@ module Rubeuler
9
9
  "5" => 0,
10
10
  "6" => 0,
11
11
  "7" => 0
12
- }[number.to_s]
12
+ }.fetch(number.to_s) { raise RuntimeError.new("Undefined solution for problem: #{number.to_s}") }
13
13
  end
14
14
  end
15
15
  end
@@ -1,3 +1,3 @@
1
1
  module Rubeuler
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,8 +1,7 @@
1
1
  require "minitest_helper"
2
- require "rubeuler"
3
2
 
4
3
  describe Rubeuler::Problem do
5
- describe ".sexecute!" do
4
+ describe ".execute!" do
6
5
  it "returns a result" do
7
6
  Rubeuler::Problem.new(number: 1, answer: "0").execute!.must_be_kind_of(Rubeuler::Result)
8
7
  end
@@ -25,10 +24,29 @@ describe Rubeuler::Problem do
25
24
 
26
25
  it "complains if :answer is not a string" do
27
26
  begin
28
- Rubeuler::Problem.new(number: 1, answer: 0)
27
+ Rubeuler::Problem.new(number: 1, answer: 0)
29
28
  rescue => e
30
- e.must_be_kind_of(TypeError)
29
+ error = e
31
30
  end
31
+ error.must_be_kind_of(TypeError)
32
+ end
33
+
34
+ it "complains if :number is not an integer" do
35
+ begin
36
+ Rubeuler::Problem.new(number: :the_letter_a, answer: "0")
37
+ rescue => e
38
+ error = e
39
+ end
40
+ error.must_be_kind_of(TypeError)
41
+ end
42
+ end
43
+
44
+ describe "#data" do
45
+ it "can track extra instance_variables" do
46
+ res = Rubeuler::Problem.new(number: 1, answer: "@foo, @baz = :bar, :bang; 111*0", tracked: {foo: "@foo", baz: "@bang"}).execute!
47
+ res.must_be_kind_of(Rubeuler::Result)
48
+ res.data.keys.include?(:foo).must_equal(true)
49
+ res.data.keys.include?(:baz).must_equal(true)
32
50
  end
33
51
  end
34
52
  end
@@ -0,0 +1,18 @@
1
+ require "minitest_helper"
2
+
3
+ describe Rubeuler::Problem do
4
+ describe ".execute!" do
5
+ it "returns a solution if predefined" do
6
+ Rubeuler::Solution.for_problem(1).must_be_kind_of(Numeric)
7
+ end
8
+
9
+ it "produces an error if key for the problem specified is not defined" do
10
+ begin
11
+ @problem = Rubeuler::Solution.for_problem(:orange)
12
+ rescue => e
13
+ e.must_be_kind_of(RuntimeError)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Rubeuler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -79,6 +79,7 @@ files:
79
79
  - rubeuler.gemspec
80
80
  - test/minitest_helper.rb
81
81
  - test/rubeuler/problem_test.rb
82
+ - test/rubeuler/solution_test.rb
82
83
  homepage: http://github.com/rthbound/rubeuler
83
84
  licenses: []
84
85
  post_install_message:
@@ -107,3 +108,4 @@ summary: Evaluates whether a block produces a predefined response. Returns diagn
107
108
  test_files:
108
109
  - test/minitest_helper.rb
109
110
  - test/rubeuler/problem_test.rb
111
+ - test/rubeuler/solution_test.rb