8/12
Today I will be jumping into Course 101 and all the content that I found somewhat difficult between lessons 2 & 4.
Lesson 2: Small Programs
One of the most important aspects of programming that I have recently learned after completing Course 101 is that Pseudo-Code or the process of being able to deconstruct a complex program into smaller and more manageable pieces is crucial in writing the code. Writing out the blueprint for the code could potentially be one of the most difficult parts of programming. Flowcharts can aid in creating the blueprint because it allows the user to visually see the flow of the program as it progresses.
After the simple calculator program, we get into refactoring the code to make it look cleaner and process better.
- Code is made cleaner by inserting methods into the original code. These methods allow the program to call back on methods that may or may not be used again later. It is also to make the code more readable.
- Breaking out of an If block that is nested in a Loop will break completely out of the If block as well as the Loop. (Not just the If block)
- Messages or text can be moved into another (configuration) file for the ruby file to access when necessary. In order to use a configuration file, create a .yml file in the same directory. Load all your text into that file and set each text line equal to a variable using a colon (:). In your ruby file, you must include “require ‘yaml’ ” at the top of the code as well as a constant variable that references the .yml file that was previously created (a. example will be provided below). Next, whenever you want to include text from the .yml file in your program, just include the name of the constant and reference the text variable in brackets (b. example will be provided below). Again, this method is used when there is a lot of text in your program and you want to transfer your text to another file for cleaner looking code.
a. CONSTANT = YAML.load_file(‘program_text.yml’)
b. CONSTANT[‘sample_text’]
The .yml configuration file can also be used to include different languages, making your program international. This is created by including nested variable within the .yml file (english: => variable: => text). This will also be called differently in the program code. (example below)
CONSTANT[‘language’][‘variable’]
Onto the Mortgage Loan Calculator Program. This was not nearly as hard as it was made out to be. The simple math was made difficult with the provided off-site guide. Otherwise, it is almost exactly the same as the calculator program.
Variable Scoping:
One of the scoping concepts I had a little bit of trouble with was the fact that methods cannot access variables outside of their scope unless the variable is passed into the method. You can only access outside variables or methods by using the #{} construct.
The Rock Paper Scissors Program:
This program was somewhat simple after completing the calculator program in that the input is a string rather than a number. The strings, therefore, need to be compared to one another to determine the winner. I took a longer approach by typing out all possible outcomes. The video provided a simpler method that included one method that was called by another method to determine the winner and to display the results. This definitely taught me to create a method that does only ONE thing, making it simple and easier to read and process.
More coding tips made it known that a method should return a value or print out a value, not both.
There are some small built-in methods that are important to know as well:
- .empty?()
- .include?()
- .start_with?()
- %w () — creates lists
- .each
- .times