Learn Ruby First
Section Two
This is the first chapter of the second section. Each new section indicates a change of scope and increase in difficulty. In section one, you were introduced to the Ruby language. Each chapter added some critical pieces of information to your understanding of Ruby. At this point, you should have a strong command of the syntax, data types, and operators. You should also feel comfortable working with classes, objects, and methods. You don't need to feel like an expert to move on but you should know the basics of how to call a method and define a class.
Section two will primarily focus on writing code. We will also learn some of the best practices developed by industry experts. These practices will help us with structuring our code and outlining the development process. They have been established by some of the most brilliant minds in the programming industry and adopted by the Ruby community as a whole. They are meant to enable developers to not only write great code today, but also maintainable and extensible code for the future.
While we are developing programs, we can be certain they will grow in size and complexity. Changes to the code are inevitable. They may come from a new feature being implemented or an enhancement to improve performance. Having the knowledge that the code we write today will need to be changed tomorrow, we can develop with extensibility in mind. Embracing change and preparing for things to come are soft skills that help turn an intermediate developer into a master of the language. In order to write fluid code, highly adaptable to change, we begin with a focus on modularity.
Write Modular Code
Applications are made up from many moving parts. Each part plays a specific role and the sum of the parts defines the finished project. Modular code is essentially code that is divided into smaller related parts. Modularity begins with having a proper project architecture.
During development it's best to keep the pieces physically separated into different folders and files. Functionality should be divided into separate modules. Each module should have just as much information as it needs to perform its job. Dependency inversion is a key concept in OOD (Object Oriented Design). The more dependencies there are the less flexible an application becomes. Brittle applications are defined as those that inflexible. They break easily when exposed to change. These applications are not extensible or future-friendly. Our goal as developers is to create quality applications, which means building with extensibility in mind.
Executing code within your editor
Many text editors and Integrated Development Environments (IDE's) have options for executing code. The Sublime editor makes this easy with a keyboard shortcut cmd + b
. If you want to execute ruby files from within Sublime, simply use the keyboard shortcut and sublime will provide a separate pane across the bottom with the results. In the image below, we see the 'add' method being executed and the correct result returned in 0.4 seconds. This method of running code is easier than the IRB shell as your code begins to span multiple lines.