Реклама

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…
Advertisement

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…

Ruby – Strings

Ruby deals with strings as well as numerical data. A string may be double-quoted (“…”) or single-quoted (‘…’). Double- and single-quoting have different effects in some cases. A double-quoted string allows character escapes by a leading backslash, and the evaluation of embedded expressions using #{}. A single-quoted string does not do this interpreting; what you see…

Ruby – Simple examples

Let’s write a function to compute factorials. The mathematical definition of n factorial is: In ruby, this can be written as: You may notice the repeated occurrence of end. Ruby has been called “Algol-like” because of this. (Actually, the syntax of ruby more closely mimics that of a langage named Eiffel.) You may also notice the lack…