The Value of Practice
I’m working my way through the pre-course ‘Learn Ruby’ online textbook at Tealeaf, and I came across a problem that I couldn’t solve without checking the answer. So after I watched the video and followed along, I created my own simple variation. It was similar to the original, but different enough to demonstrate that I’d internalized the lesson.
I like writing code because it’s all about solving problems, and it can be incredibly expressive. But learning anything requires us to move just beyond our current capabilities, and apply what we know to new problems. Here is a simple example of how I did that.
The exact details of the problem are not that interesting, but essentially, Exercise 5 asked me to rewrite an if/else method (‘evaluate_num’), which I’d made for Exercise 3, as a case method, using two different forms of the syntax (‘evaluate_case1_num’ and ‘evaluate_case2_num’). I had some difficulty, so I checked the solution.
Here is the authors’ solution:
1 # evaluate_num_revisited.rb
2
3 def evaluate_num(num)
4 if num < 0
5 puts "You can't enter a negative num!"
6 elsif num <= 50
7 puts "#{num} is between 0 and 50"
8 elsif num <= 100
9 puts "#{num} is between 51 and 100"
10 else
11 puts "#{num} is above 100"
12 end
13 end
14
15 def evaluate_case1_num(num)
16 case
17 when num < 0
18 puts "You can't enter a negative num!"
19 when num <= 50
20 puts "#{num} is between 0 and 50"
21 when num <= 100
22 puts "#{num} is between 51 and 100"
23 else
24 puts "#{num} is above 100"
25 end
26 end
27
28 def evaluate_case2_num(num)
29 case num
30 when 0..50
31 puts "#{num} is between 0 and 50"
32 when 51..100
33 puts "#{num} is between 51 and 100"
34 else
35 if num < 0
36 puts "You can't enter a negative num!"
37 else
38 puts "#{num} is above 100"
39 end
40 end
41 end
42
43 puts "Please enter a number between 0 and 100."
44 number = gets.chomp.to_i
45
46 evaluate_num(number)
47 evaluate_case1_num(number)
48 evaluate_case2_num(number)
After I saw this and watched the video, I understood what I’d been doing wrong, but I wasn’t sure if I could apply what I’d learned. So I created my own original version. It’s very similar, but instead of asking for a number, it asks the user to talk about his/her day. The feedback the user gets depends on the length of the answer.
The point of doing three versions is to demonstrate three ways to solve the same problem, using both ‘if/else’ statements and ‘case’ statements. My program follows the same form, but makes a few changes. For example, in the string_length method:
- Instead of asking for a number, it lets you type in whatever you want, and it counts the number of characters in your answer.
- It provides three answer options, depending on the length of your answer.
def string_length(num)
if num == 0
puts "You have to give me something to work with, here."
elsif num <= 10
puts "You're not much with words, are you?"
elsif num <= 20
puts "You've got a lot to say."
else
puts "Sounds like you had quite a day!"
end
end
- If the user provides no answer, the program replies, “You have to give me something to work with here.”
- If the user’s answer is 10 characters or less, the program says, “You’re not much with words, are you?”
- For answers of 11 - 20 characters, the response is: “Sounds like you had quite a day!”
- Answers with 21 or more characters prompt the reply, “Sounds like you had quite a day!”
Here’s the full version, with the three versions that perform the same behavior:
1 # I had trouble figuring out 05_flow_control/evaluate_num.rb on my own, so I created a new version for practice.
2 # This program asks how your day was. The length of your answer determines the reply!
3 def string_length(num)
4 if num == 0
5 puts "You have to give me something to work with, here."
6 elsif num <= 10
7 puts "You're not much with words, are you?"
8 elsif num <= 20
9 puts "You've got a lot to say."
10 else
11 puts "Sounds like you had quite a day!"
12 end
13 end
14
15 def string_length_case1(num)
16 case
17 when num == 0
18 puts "You have to give me something to work with, here."
19 when num <= 10
20 puts "You're not much with words, are you?"
21 when num <=20
22 puts "You've got a lot to say."
23 else
24 puts "Sounds like you had quite a day!"
25 end
26 end
27
28 def string_length_case2(num)
29 case num
30 when 1..10
31 puts "You're not much with words, are you?"
32 when 11..20
33 puts "You've got a lot to say."
34 else
35 if num == 0
36 puts "You have to give me something to work with, here."
37 else
38 puts "Sounds like you had quite a day!"
39 end
40 end
41 end
42
43 puts "Tell me about your day."
44 number = gets.chomp.to_s.length
45
46 string_length(number)
47 string_length_case1(number)
48 string_length_case2(number)
The code itself is very simple. But it’s important to study good examples, and then use them as a basis for making new and more complex discoveries. And that is how we learn! I was a teacher before, and I have always been interested in understanding how the mind works, and how to improve my ability to learn. One of the most influential scholars, whose theories have resonated with my own experience of learning, is the Psychologist and Learning Theorist, Lev Vgotsky. He posited the idea of a “Zone of Proximal Development,” which was just beyond the current level of a student’s ability to perform learning tasks.
At the risk of oversimplifying, this means that learning occurs when we move from a point where we cannot perform tasks without help, to a point where we can do them on our own (A.K.A. our Zone of Proximal Development). With the proper support (‘scaffolding’) to build this competency, I have no doubt I’ll keep learning for the rest of my life! This is just one example of how I am building my knowledge of Ruby every day. I have a lot to learn, but every day, I take another step…
If you enjoyed this post, you might want to subscribe, so you don't miss the next one!