
An Example Of Computational Thinking With Ruby
Share

Published
12 Oct 2016
Segment
coding starters
Duration
15 minute read
When learning to code you hear a lot about Computational Thinking. But, what does that mean? Let us break it down for you with an example of Ruby code. This example is a little esoteric, but it’s still simple, so bear with me.
A good way to start with computational thinking is to break down some of Ruby’s default methods, and try and replicate them. They are fairly simple, and can be seen as a combination of various steps, even a combination of various methods. We’re going to back to first principles here. (Learning to code is a bit like that.)
For example, reversing. Think about the things you can do with Ruby. You’ll find out that you can inject characters into a string, you can unshift and pop characters out of a string, you can split strings by predetermined characters, and you can count the number of characters in a string. Now, look them up, and think how you could reverse a string with those actions. You don’t have to use them all.
Here’s some examples of splitting strings to show you what can be done. Splitting strings looks like this:
By default it splits by spaces.
You can make it other things.
You’ll notice that .split removes the characters it splits by.
From here on, everything on the left side of a table will be code, and the right side will be explanations of what’s going on, so you can see the decomposition work.
Real Life example.
You’ll notice this simple process still has quite a lot of steps!
For example, you don’t just ‘walk’, you put your right leg forward, and your right foot onto the ground, and then do the same with your left until you reach your destination. If you fall or stop, you don’t reach your destination. That’s coding for you...
So here’s a new concept: The best way to do reverse with Ruby is with the Ruby way. Let’s start with what that does NOT look like.
Well for starters, it doesn’t look like this:
def reverse_string(string) |
Create a new method, reverse_string with parameter 'string' (this could also be confusing later!). |
loop = string.length |
Assert that int loop is equal to the string's length. |
word = '' |
We will use this newly created empty string to output the reversed word. |
while loop > 0 |
while loop is greater than 0, subtract loop by 1 and add the string's index of loop to 'word'. |
loop -= 1 |
Subtract 1 character from the loop |
word += string[loop] |
Add the index with the int loop to word. |
end |
End the loop |
return word |
Return the newly fully formed string |
end |
End the class |
The Ruby Way
It works, but this is more a C or Python way to do things. Why would you do things the ‘Ruby way?’ It is easily recognisable to people reading your code. It works as Ruby, so it should aim to look like Ruby. This way it won’t increase cognitive load and make the software just a little harder to read and conceptualise.
It’s like that game where you see a bunch of colour names, and they’re coloured in a colour that isn’t their name, like RED being painted RED. That’s exactly what improperly formatted code can be like to read, and that’s why you’ll see ferocious debates on, for example, ‘tabs vs spaces’, and the placement of brackets.
What looks like Ruby?
This looks like Ruby.
class String |
Create a class and call it String. |
def rString |
Make a method. |
arr = self.split("") |
Split your string by every character |
len = arr.count - 1 |
Use default method count to count the number of characters in the string |
final = "" |
Create an empty string to hold the result |
arr.each_index do |i| |
Every loop, pass the index of the array |
final += arr[len - i] |
The new, now less empty string |
end |
|
return final |
|
end |
|
End |
This also looks like Ruby, and it’s very concise. This is something you’d work up to. It’s more important when you’re learning to code to know more about what’s going on than focus on making it ‘terse’.
def reverse(s) |word, char| word.unshift char }.join What’s happening c = “Octopus” word = ”s” word = ”su” word = ”sup” word = ”supo” word = ”supot” word = ”supotc” word = ”suputcO” |
With this code block, you are using reduce, which iterates through an array and and keeps and modifies a running total along the way. The method takes two parameters, word and char. For every run of reduce, a character is unshifted, that is, characters are prepended to the front of word, moving other elements upwards. .join then concatenates the array of characters into a string. |
Stacks and Queues
The language of much of this is based on the computer science principle of stacks and queues. It’s well worth reading about. Understand concepts like Last In First Out, and First In First Out, and you’ll know some of the mechanics of back-end development, like working out which server requests to prioritize, and pretty much all kinds of systems where you have to prioritize, but you still eventually have to let all requests go through. These are some of the questions you think about as you scale a website for increasing numbers of users.
Want to become a junior developer? At Coder Academy we have Australia's first and only accredited fast-track coding bootcamp that will set you up for a new career in tech. Our immersive course helps students acquire in-demand skills through hands on, project-based training by industry experts over six months.
Now enrolling domestic & international students in Sydney, Melbourne & Brisbane! Study now, pay later with FEE-HELP!
Are you a woman interested in coding? Check out our Women in Tech Scholarship!
BECOME A FULL STACK DEVELOPER IN 25 WEEKS OF STUDY
Seriously considering a coding bootcamp? Learn more about Australia's first accredited coding bootcamp.