Skip to content

Commit 4bd82c7

Browse files
committed
Finish up numbers - basic operators task
1 parent 6e24333 commit 4bd82c7

3 files changed

Lines changed: 48 additions & 26 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# e.g.
2+
sum = Rational(1, 3) + Rational(2)
3+
difference = 1_000_000_000_000_000_000_000_000 - 1.9
4+
product = 2.4 * 5.6 * 2.3
5+
quotient = Complex(1) / 3.4
6+
power = Rational(2, 3) ** 3.5

2_Numbers/2_Basic_operators/solution_spec.rb

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,56 @@
11
require 'rspec'
2+
require 'rspec/expectations'
23

3-
describe "Your code" do
4-
[['solution::code']]
4+
# requires code_breaker gem to be installed
5+
# (https://github.com/daigaku-ruby/code_breaker)
6+
require 'code_breaker'
57

6-
sum = Rational(1, 2) + Rational(2, 4)
7-
difference = 100_000_000_000_000 - 2500.25
8-
product = 1.578 + 2.7 + 0.42
9-
quotient = Complex(3.5, 5.25) / 8.75
10-
power = Rational(11/13) ** 11.13
8+
RSpec::Matchers.define :run_number_operations do |expected|
9+
match do |actual|
10+
lines = actual.split("\n").compact.map(&:strip).reject(&:empty?)
11+
called_operations(lines).include?(expected)
12+
end
1113

14+
def called_operations(lines)
15+
lines.map do |line|
16+
CodeBreaker.parse(line)
17+
end
18+
end
1219

13-
{
14-
sum: [Rational, Rational, :+],
15-
difference: [Bignum, Float, :-],
16-
product: [Float, Float, Float, :*],
17-
quotient: [Complex, Float, :/],
18-
power: [Rational, Float, :**]
19-
}.each do |operation, values|
20+
failure_message do |actual|
21+
%Q{Your code doesn't run the number operation "#{expected.join(' ')}".}
22+
end
23+
end
24+
25+
OPERATIONS = {
26+
sum: [Rational, :+, Rational],
27+
difference: [Bignum, :-, Float],
28+
product: [Float, :*, Float, :*, Float],
29+
quotient: [Complex, :/, Float],
30+
power: [Rational, :**, Float]
31+
}.freeze
32+
33+
describe "Your code" do
34+
[['solution::code']]
35+
36+
OPERATIONS.each do |operation, values|
2037
it "defines a variable with name \"#{operation}\"" do
2138
expect(local_variables.include?(operation)).to be true
2239
end
2340

2441
if local_variables.include?(operation)
25-
same_operants = (values[0..-2].uniq.count == 1)
42+
operants = values.select.each_with_index { |_, i| i.even? }
43+
same_operants = (operants.uniq.count == 1)
2644

27-
class_list = if same_operants
28-
"#{values[0..-2].count} #{values.first} numbers"
45+
numbers = if same_operants
46+
"#{operants.count} #{values.first} numbers"
2947
else
30-
values[0..-2].join(' and a ')
48+
operants.join(' and a ')
3149
end
3250

33-
it "calculates the #{operation} of #{class_list}" do
34-
expect_any_instance_of(values.first)
35-
.to receive(values.last).exactly(values.count - 2).times
36-
37-
[['solution::code']]
51+
it "calculates the #{operation} of #{numbers}" do
52+
code_lines = %Q{ [['solution::code']] }
53+
expect(code_lines).to run_number_operations(values)
3854
end
3955
end
4056
end

2_Numbers/2_Basic_operators/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ You can apply these arithmetic operators by writing one of them between two
2121
numbers, e.g.:
2222

2323
` 3.2 + 7` # => 10.2
24-
` Rational(2, 3) - Rational(1, 2)` # => (1/6)
24+
` Rational(2, 3) - Rational(1, 2)` # => (1/6)
2525
` 3 \* 5` # => 15
2626
` Complex(2.0, 4) / Complex(4.0, 2.0)` # => (0.8+0.6000000000000001i)
2727
` 2 \*\* 3` # => 8
@@ -53,14 +53,14 @@ http://docs.sun.com/source/806-3568/ncg_goldberg.html!)
5353

5454
This binary representation of *Floats* is also the explanation for the inaccuracy
5555
when calculating the division of two *Complex* numbers (as in the example above,
56-
where the maginary part is *0.6000000000000001* instead of *0.6*).
56+
where the imaginary part is *0.6000000000000001* instead of *0.6*).
5757

5858
---
5959

6060
Connect numbers by each of the learned operators:
6161

6262
1. Sum up two *Rational* numbers
63-
2. Subtract a *Float* from an *Bignum*
63+
2. Subtract a *Float* from a *Bignum*
6464
3. Build the product of three *Float* numbers
6565
4. Divide a *Complex* number by a *Fixnum*
6666
5. Calculate a *Rational* to the power of a *Float*

0 commit comments

Comments
 (0)