My learning and challenges so far
Over the last couple weeks I've learnt a thing or two not only about the core workings and features of ruby as a language, but also about how to manage all my files and the git repositories I keep them in through the command line.
While at first I was getting lost in the march of 'no such file or directory' messages, I have now come to a basic understanding of navigating files on the command line. I have even completed a fun 'murder mystery' challenge which really emphasised the use of grep and related commands to extract and store relevant information to solve the mystery.
One can imagine how hard it must be to search a file for a crime scene report buried within the full text of 'Alice in Wonderland' without some command line sorcery!
While I still have many more tips and tricks to pick up in order to really make full use of the command line, these should come in time.
I've also tried my hand at doing a range of interesting and challenging things with code itself, namely ruby. The most unusual of these so far was to take an array containing various strings representing 'levels' of a river and create a game wherein the user (represented by 'P') navigates down the river and must avoid bumping into ravenous crocodiles (represented by 'C's). At this point I have some basic understanding of all the main structures (arrays, hashes, loops) but still have yet to really make full use of classes, lambdas or procs.
A couple things so far that have really stuck out or been useful to me about ruby as a language are:
Many methods reserved for arrays can also be used on strings, treating them as 'arrays' of characters.
There are all manner of interesting alternative syntaxes, such as the format 'condition ? "outcome_a" : "outcome_b"' for an if-else statement or the use of 'array << element' instead of 'array.push(element)'. However, they are only more elegant in some contexts!
The language feels a lot more elegant and approachable than Java. The latter I hope to get used to in time.
I've also noticed a couple things I still need to get used to:
Making sure I don't use '=' where I should be using '==', and proofreading in general
Getting some more built-in methods under my belt! There are many with a number of valuable uses, but I don't know many off by heart so my capabilities are still limited.
Ensuring proper flow of control with many methods. The penultimate exercise of weeks 2-3 of my precourse, programming a game of blackjack, challenged me a lot for this reason.
I found it difficult to ensure that methods were communicating with each other properly in terms of transferability of variables and return of information from variables.
My current project
More recently as part of my pre-course, the time has come for me to try my hand at building something more elaborate: a directory of student names! So far, I have implemented and played with a couple of features.
A very basic way to do this with only rudimentary coding knowledge would be to 'puts' each and every name as a string on a new line, and then 'puts' a string declaring the number of students.
The intuitive and first model for such a program is instead to store the names all in an array as I did, and iterate 'puts' over each of these when we run the program.
But we can also, in turn, make the student count statement dynamic and able to stay accurate to the actual student count automatically.
This I did by calling '.length' on the student array within the string via interpolation, which resembles this:
'puts "We currently have #{array.length} students total"'
I went even further and created three seperate methods for printing a header, the list of students, and a footer stating the number of students respectively.
But what if, in turn, we wanted to include some extra information? Say I wanted to store not just a name, but a cohort month? For this, a possible solution is to store hashes in our array.
Hashes differ from arrays in that you typically call the 'values' not by numerical index, but by keys. These can be integers, strings, symbols and other objects.
Multiple different keys in an array can be assigned to the same value, but in this particular case I made use of the fact that the same key can be used to return a different value per array (two different values on the same key doesn't make as much sense within a hash).
A 'name' key can be used on each hash representing each student to return their names, and a 'month' key to return their cohort.
From this we can fill our array with a hash per student, instead of strings of their names, and plug the same keys into each such hash to return the value that each hash uniquely assigns to said key, as follows:
"array.each {|student| puts "#{student[name]},#{student[month]}"
I never thought I'd see the day when I built an array of hashes, over which I would subsequently iterate with a side-serving of string interpolation, but here we are. And we're not done yet!
A set of features I'm looking to implement as part of my learning exercises over the rest of the week are:
Getting user input: perhaps to add or remove students, or even change their month cohort if there's been a mix up!
Even more information per student
Centred text if possible
An interactive menu allowing users to navigate and call the different methods of the program at will
Saving output from the program into files
And more!
How exactly I am going to do all of this is something I am excited to find out along my journey through the world of code!
~ Charlie Paterson
Top comments (0)