Реклама

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

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…

Ruby – Getting Started

First, you’ll want to check whether ruby is installed. From the shell prompt (denoted here by “%“, so don’t type the %), type (-v tells the interpreter to print the version of ruby), then press the Enter key. If ruby is installed, you will see a message something like the following: If ruby is not…

What is Ruby?

Ruby is “an interpreted scripting language for quick and easy object-oriented programming”; what does this mean? interpreted scripting language: ability to make operating system calls directly powerful string operations and regular expressions immediate feedback during development quick and easy: variable declarations are unnecessary variables are not typed syntax is simple and consistent memory management is…