- The Ruby Workshop
- Akshat Paul Peter Philips Dániel Szabó Cheyne Wallace
- 434字
- 2021-06-11 13:04:42
Introduction
In the previous chapter, we learned about Boolean variables, conditional expressions, and loops, including the core Ruby concepts of using blocks. We will now zoom out a little in the Ruby world and learn about methods. In fact, we've been using methods in the previous chapters, and we will look at them in more depth now.
Methods are foundational units in Ruby. They allow you to wrap code in chunks that can be called with different parameters to get different outputs. Methods commonly have descriptive names and, as such, make clear what the underlying bundle of code does. For instance, we previously learned about the each method on arrays. This method enumerates over each item in the array, and so is very descriptive. In fact, method naming is an art where you balance simplicity with descriptiveness. It is considered that the difficult things in computer science are caching, naming things, and off-by-one errors. Commonly known as OBOB errors, off-by-one bugs are logical errors that occur when there are too many iterations in a program or when there are mistakes in the comparisons in code.
Methods are like atoms. They have can have internal parts; however, for the most part, they are fundamental units of code with a defined interface. In addition to this, methods are the building blocks for classes, which are the higher-order building blocks for Ruby programs.
Methods should be designed so that they are simple and can accomplish a basic purpose. If a method gets too long or complicated, then that is a good sign that you need to break up the method into smaller, more clearly labeled methods. By having clearly labeled single-purpose methods, readability, maintainability, and testability are increased. These three attributes are the cornerstones of great code.
Additionally, by defining a clear interface to the method, we can change the implementation at any time without fear of breaking any code that calls the method. In Ruby, this can be a bit trickier because Ruby is a dynamically typed language. This means that the arguments passed to a method or the values returned from a method can be of any type. For instance, a method can accept a string or an integer or any other type of object in its arguments. It's up to the method's implementation to handle each of these cases. A common approach to handling all these cases is actually not to worry about the specific types of arguments, but to worry about whether the arguments behave as we need them to instead. This is called duck typing and will be covered in this chapter.
- 基于粒計(jì)算模型的圖像處理
- Visual Basic編程:從基礎(chǔ)到實(shí)踐(第2版)
- Python高級(jí)編程
- Learning Linux Binary Analysis
- 游戲程序設(shè)計(jì)教程
- 精通網(wǎng)絡(luò)視頻核心開(kāi)發(fā)技術(shù)
- PhpStorm Cookbook
- 精通Python設(shè)計(jì)模式(第2版)
- SAP BusinessObjects Dashboards 4.1 Cookbook
- Java:High-Performance Apps with Java 9
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- .NET 4.5 Parallel Extensions Cookbook
- 零基礎(chǔ)學(xué)C語(yǔ)言(升級(jí)版)
- Arduino電子設(shè)計(jì)實(shí)戰(zhàn)指南:零基礎(chǔ)篇
- Web前端測(cè)試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實(shí)踐