Реклама

Ruby – Redefinition of methods

In a subclass, we can change the behavior of the instances by redefining superclass methods. Suppose we would rather enhance the superclass’s identifity method than entirely replace it. For this we can use super. super lets us pass arguments to the original method. It is sometimes said that there are two kinds of people…

Ruby – Inheritance

Our classification of objects in everyday life is naturally hierarchical. We know that all cats are mammals, and all mammals are animals. Smaller classes inherit characteristics from the larger classes to which they belong. If all mammals breathe, then all cats breathe. We can express this concept in ruby: Though we didn’t specify how a Cat should…
Advertisement

Ruby – classes

The real world is filled by objects, and we can classify them. For example, a very small child is likely to say “bow-wow” when seeing a dog, regardless of the breed; we naturally see the world in terms of these categories. In OO programming terminology, a category of objects like “dog” is called a class,…

Ruby – methods

What is a method? In OO programming, we don’t think of operating on data directly from outside an object; rather, objects have some understanding of how to operate on themselves (when asked nicely to do so). You might say we pass messages to an object, and those messages will generally elicit some kind of an…

Ruby – Object-oriented thinking

Object oriented is a catchy phrase. To call anything object oriented can make you sound pretty smart. Ruby claims to be an object oriented scripting language; but what exactly does “object oriented” mean? There have been a variety of answers to that question, all of which probably boil down to about the same thing. Rather…

Ruby – iterators

Iterators are not an original concept with ruby. They are in common use in object-oriented languages. They are also used in Lisp, though there they are not called iterators. However the concepet of iterator is an unfamiliar one for many so it should be explained in more detail. The verb iterate means to do the…

Ruby – control structures

This chapter explores more of ruby’s control structures. case We use the case statement to test a sequence of conditions. This is superficially similar to switch in C and Java but is considerably more powerful, as we shall see. 2..5 is an expression which means the range between 2 and 5, inclusive. The following expression tests whether…

Ruby – Back to the simple examples

Now let’s take apart the code of some of our previous example programs. The following appeared in the simple examples chapter. Because this is the first explanation, we examine each line individually. Factorials In the first line, def is a statement to define a function (or, more precisely, a method; we’ll talk more about what a method…

Ruby – arrays

You can create an array by listing some items within square brackets ([]) and separating them with commas. Ruby’s arrays can accomodate diverse object types. Arrays can be concatenated or repeated just as strings can. We can use index numbers to refer to any part of a array. (Negative indices mean offsets from the end of…

Ruby – Regular expressions

Let’s put together a more interesting program. This time we test whether a string fits a description, encoded into a concise pattern. There are some characters and character combinations that have special meaning in these patterns, including: [] – range specificication (e.g., [a-z] means a letter in the range a to z) \w – letter or…