Skip to content

Commit 16dfefd

Browse files
committed
Add Strings chapter and Interpolation unit
1 parent 102089e commit 16dfefd

6 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# e.g.
2+
firstname = 'Peter'
3+
lastname = 'Pan'
4+
fullname = "#{lastname}, #{firstname}"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require 'rspec'
2+
require 'rspec/expectations'
3+
require 'code_breaker'
4+
5+
RSpec::Matchers.define :run_string_interpolation do |expected|
6+
match do |actual|
7+
interpolation = interpolation_part_of(actual)
8+
return false if interpolation.nil?
9+
interpolation[:lvasgn][1][:dstr] == expected
10+
end
11+
12+
failure_message do |actual|
13+
actual_line = actual.split("\n").select do |line|
14+
line =~ /fullname\s?=.+/
15+
end
16+
17+
parsed_line = CodeBreaker.parse(actual_line.join)
18+
interpolation = parsed_line[:lvasgn].last
19+
20+
unless interpolation.is_a?(Hash) && interpolation[:dstr]
21+
return "You didn't do any string interpolation."
22+
end
23+
24+
[['solution::code']]
25+
26+
%Q{Your interpolated fullname shoud be "#{lastname}, #{firstname}"
27+
------- but is "#{fullname}".}
28+
end
29+
30+
def interpolation_part_of(actual)
31+
parsed = CodeBreaker.parse(actual)
32+
33+
interpolation = parsed.select do |part|
34+
next unless part.is_a?(Hash)
35+
next unless part.has_key?(:lvasgn)
36+
next unless part[:lvasgn][0] == :fullname
37+
next unless part[:lvasgn][1].is_a?(Hash)
38+
39+
part[:lvasgn][1].has_key?(:dstr)
40+
end
41+
42+
interpolation.first
43+
end
44+
end
45+
describe "Your code" do
46+
[['solution::code']]
47+
48+
VARIABLES = [:firstname, :lastname, :fullname].freeze
49+
50+
VARIABLES.each do |name|
51+
it "defines a variable with name \"#{name}\"" do
52+
expect(local_variables.include?(name)).to be true
53+
end
54+
55+
if name != :fullname && local_variables.include?(name)
56+
it "assigns a String to the variable \"#{name}\"" do
57+
expect(eval(name.to_s)).to be_a String
58+
end
59+
end
60+
end
61+
62+
if (local_variables & VARIABLES).sort == VARIABLES.sort
63+
it "creates an interpolated String" do
64+
statement = [[{ lvar: :lastname }], String, [{ lvar: :firstname }]]
65+
code_lines = %q{ [['solution::code']] }
66+
67+
expect(code_lines).to run_string_interpolation(statement)
68+
end
69+
end
70+
end

3_Strings/1_Interpolation/task.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Interpolation
2+
3+
---
4+
5+
*You will learn:*
6+
- how to create interpolated Strings
7+
8+
---
9+
10+
You already learned that you can define a String with either single quotes or
11+
double quotes. The resulting String is the same, but if you use the double quotes
12+
for creating a String you can interpolate variables within this string.
13+
14+
The interpolated statement has to be wrapped in *#{}* – a hash followed by curly brackets.
15+
Let's look at an example:
16+
17+
` amount = 5`
18+
` message = "I learned` *#{amount}* `units today."`
19+
20+
The variable `message` now has the value *"I learned 5 units today."*.
21+
If you would use single quotes then the String would not evaluate the *#{}* part:
22+
23+
` message = 'I learned #{amount} units today.'`
24+
25+
The variable `message` has the value *"I learned #{amount} values today."*
26+
the value of `amount` will not be interpolated into the String.
27+
28+
A common pattern is:
29+
30+
\* You should use single quotes `''` for Strings that are not interpolated
31+
\* You should only use double quotes `""` for Strings that should be interpolated
32+
33+
---
34+
35+
Define a variable `firstname` and `lastname` with your firstname and lastname as values.
36+
Then create a variable `fullname` and assign an interpolated String by using
37+
the `lastname` and `firstname` variables.
38+
39+
The resulting String should look like *"YourLastname, YourFirstname"*.
40+
41+
---
File renamed without changes.

0 commit comments

Comments
 (0)