Skip to content

Commit bdb03dc

Browse files
committed
Merge pull request #2 from daigaku-ruby/hotfix/numbers-chapter
Hotfix/numbers chapter
2 parents 102089e + 2f1ec9f commit bdb03dc

4 files changed

Lines changed: 29 additions & 16 deletions

File tree

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# e.g.:
2-
fixnum = 5
3-
bignum = 100_000_000_000_000_000_000
2+
fixnum = 5
3+
bignum = 100_000_000_000_000_000_000
4+
float = 8.25
45
rational = Rational(2, 7)
5-
complex = Complex(0.1, 2.5)
6+
complex = Complex(0.1, 2.5)

2_Numbers/1_The_different_types_of_numbers/solution_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
[['solution::code']]
55

66
{
7-
fixnum: Fixnum,
8-
bignum: Bignum,
7+
fixnum: Fixnum,
8+
bignum: Bignum,
9+
float: Float,
910
rational: Rational,
10-
complex: Complex
11+
complex: Complex
1112
}.each do |variable, class_name|
1213
it "defines a variable with name \"#{variable}\"" do
1314
expect(local_variables.include?(variable)).to be true

2_Numbers/1_The_different_types_of_numbers/task.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ If you want to learn more, read about Fixnum and Bignum here:
4444
A *Float* represents an *inexact real number*.
4545
You can create a Float by declaring a number with a decimal place, like `3.0`
4646
or `-2_000.25`.
47+
4748
Another way to create a Float is to call `Float()` with a parameter, e.g.:
4849

4950
` Float(3)` *# => 3.0*
@@ -58,7 +59,7 @@ You can create a Rational by calling `Rational()` with one or two parameters, e.
5859

5960
` Rational(2)` *# => (2/1)*
6061
` Rational(2, 3)` *# => (2/3)*
61-
` Rarional('2/3')` *# => (2/3)*
62+
` Rational('2/3')` *# => (2/3)*
6263

6364
Read more about Rational here: (ruby-doc core: Rational).
6465

2_Numbers/2_Basic_operators/solution_spec.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,35 @@
44

55
RSpec::Matchers.define :run_number_operations do |expected|
66
match do |actual|
7-
CodeBreaker.parse(actual).include?(expected)
7+
actual_code = replace_explicit_numbers(actual)
8+
CodeBreaker.parse(actual_code).include?(expected)
89
end
910

1011
failure_message do |actual|
11-
expected_operants = expected.values.first
12-
variable_name = expected_operants[0]
12+
expected_operants = expected.values.first
13+
variable_name = expected_operants[0]
1314
expected_operation = "#{variable_name} = #{expected_operants[1].join(' ')}"
1415

1516
actual_line = actual.split("\n").select do |output|
16-
output.match(/#{variable_name} =.+/)
17+
output.match(/#{variable_name}\s?=.+/)
1718
end
1819

19-
actual_parsed = CodeBreaker.parse(actual_line.join)
20-
actual_operants = actual_parsed.values.first
21-
actual_operation = "#{variable_name} = #{actual_operants[1].join(' ')}"
20+
actual_code = replace_explicit_numbers(actual_line.join)
21+
actual_parsed = CodeBreaker.parse(actual_code)
22+
23+
actual_operants = actual_parsed.values.first
24+
actual_operation = "#{variable_name} = #{Array(actual_operants[1]).join(' ')}"
2225

2326
%Q{Your code doesn't run the number operation "#{expected_operation}".
2427
------- Instead you calculated "#{actual_operation}".}
2528
end
29+
30+
def replace_explicit_numbers(actual)
31+
evaluate_match = lambda { |_| eval($1) }
32+
integer_or_float = /((Integer|Float)\(['"]?([-+]?\d*(_\d{1,})*(\.\d*)?)['"]?\))/
33+
34+
actual.gsub(integer_or_float, &evaluate_match)
35+
end
2636
end
2737

2838
# code_breaker outputs
@@ -39,14 +49,14 @@
3949

4050
OPERATIONS.each do |operation|
4151
variable_name = operation.first.last.first
42-
values = operation.values.first.last
52+
values = operation.values.first.last
4353

4454
it "defines a variable with name \"#{variable_name}\"" do
4555
expect(local_variables.include?(variable_name)).to be true
4656
end
4757

4858
if local_variables.include?(variable_name)
49-
operants = values.select.each_with_index { |_, i| i.even? }
59+
operants = values.select.each_with_index { |_, i| i.even? }
5060
same_operants = (operants.uniq.count == 1)
5161

5262
numbers = if same_operants

0 commit comments

Comments
 (0)