CHAPTER 10

image

Functions and Methods

We have learned a lot about the Ruby programming language itself in the previous chapters along with its syntax and type characteristics. In this chapter we begin to look at “reusable code,” the ability to write a snippet of code that can be used multiple times during our application. This is sometimes known as the D.R.Y. (Don’t Repeat Yourself) principle.

Technically, because Ruby is a fully object-oriented language, Ruby’s functions are actually all methods, as they are all linked to objects.

Defining and Calling Methods

To call our Hello World multiple times, we will write a simple method named hello, and will ...

Get Ruby Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.