1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="roadmap.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="tut_classes.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<h1>Ruby.new</h1><hr><br>
<P></P>
When we originally wrote this book, we had a grand plan (we were
younger then). We wanted to document the language from the top down,
starting with classes and objects, and ending with the nitty-gritty
syntax details. It seemed like a good idea at the time. After all,
most everything in Ruby is an object, so it made sense to talk about
objects first.
<P></P>
Or so we thought.
<P></P>
Unfortunately, it turns out to be difficult to describe a language
that way. If you haven't covered strings, <code>if</code> statements,
assignments, and other details, it's difficult to write examples of
classes. Throughout our top-down description, we kept coming across
low-level details we needed to cover so that the example code would
make sense.
<P></P>
So, we came up with another grand plan (they don't call us pragmatic
for nothing). We'd still describe Ruby starting at the top. But before
we did that, we'd add a short chapter that described all the common
language features used in the examples along with the special vocabulary
used in Ruby, a kind of minitutorial to bootstrap us into the rest of
the book.
<h2>Ruby Is an Object-Oriented Language</h2>
<P></P>
Let's say it again. Ruby is a genuine object-oriented language.
Everything you manipulate is an object, and the results of those
manipulations are themselves objects. However, many languages make the
same claim, and they often have a different interpretation of what
object-oriented means and a different terminology for the concepts
they employ.
<P></P>
So, before we get too far into the details, let's briefly look at the
terms and notation that <em>we'll</em> be using.
<P></P>
When you write object-oriented code, you're normally looking to model
concepts from the real world in your code. Typically during this modeling
process you'll discover categories of things that need to be
represented in code. In a jukebox, the concept of a ``song'' might be
such a category. In Ruby, you'd define a <em>class</em> to represent
each of these entities. A class is a combination of state (for
example, the name of the song) and methods that use that state (perhaps
a method to play the song).
<P></P>
Once you have these classes, you'll typically want to create a number
of <em>instances</em> of each. For the jukebox system containing a class
called <code>Song</code>, you'd have separate instances for popular hits such
as ``Ruby Tuesday,'' ``Enveloped in Python,'' ``String of Pearls,''
``Small talk,'' and so on. The word <em>object</em> is used
interchangeably with class instance (and being lazy typists, we'll
probably be using the word ``object'' more frequently).
<P></P>
In Ruby, these objects are created by calling a constructor, a special
method associated with a class. The standard constructor is called
<code>new</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
song1 = Song.new("Ruby Tuesday")
song2 = Song.new("Enveloped in Python")
# and so on
</pre></td></tr></table>
<P></P>
These instances are both derived from the same class, but they have
unique characteristics. First, every object has a unique <em>object
identifier</em> (abbreviated as <em>object id</em>). Second, you can
define <em>instance variables</em>,
variables with values that are
unique to each instance. These instance variables hold an object's
state. Each of our songs, for example, will probably have an instance
variable that holds the song title.
<P></P>
Within each class, you can define <em>instance methods</em>.
Each method
is a chunk of functionality which may be called from within the class
and (depending on accessibility constraints) from outside. These
instance methods in turn have access to the object's instance
variables, and hence to the object's state.
<P></P>
Methods are invoked by sending a message to an object.
The message
contains the method's name, along with any parameters the method may
need.<em>[This idea of expressing method calls in the form of
messages comes from Smalltalk.]</em> When an object receives a message,
it looks into its own class for a corresponding method. If found, that
method is executed. If the method <em>isn't</em> found, ... well,
we'll get to that later.
<P></P>
This business of methods and messages may sound complicated, but in
practice it is very natural. Let's look at some method calls.
(Remember that the arrows in the code examples show the values
returned by the corresponding expressions.)
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>"gin joint".length</code></td>
<td valign="top"></td>
<td valign="top"><code>9</code></td>
</tr>
<tr>
<td valign="top"><code>"Rick".index("c")</code></td>
<td valign="top"></td>
<td valign="top"><code>2</code></td>
</tr>
<tr>
<td valign="top"><code>-1942.abs</code></td>
<td valign="top"></td>
<td valign="top"><code>1942</code></td>
</tr>
<tr>
<td valign="top"><code>sam.play(aSong)</code></td>
<td valign="top"></td>
<td valign="top"><code>"duh dum, da dum de dum ..."</code></td>
</tr>
</table>
<P></P>
<P></P>
Here, the thing before the period is called the <em>receiver</em>, and
the name after the period is the method to be invoked.
The first
example asks a string for its length, and the second asks a different
string to find the index of the letter ``c.'' The third line has a
number calculate its absolute value. Finally, we ask Sam to play us
a song.
<P></P>
It's worth noting here a major difference between Ruby and most other
languages. In (say) Java, you'd find the absolute value of some number
by calling a separate function and passing in that number. You might
write
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
number = Math.abs(number) // Java code
</pre></td></tr></table>
<P></P>
In Ruby, the ability to determine an absolute value is built into
numbers---they take care of the details internally. You simply send
the message <code>abs</code> to a number object and let it do the work.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
number = number.abs
</pre></td></tr></table>
<P></P>
The same applies to all Ruby objects: in C you'd write
<code>strlen(name)</code>, while in Ruby it's <code>name.length</code>, and so
on. This is part of what we mean when we say that Ruby is a genuine OO
language.
<h2>Some Basic Ruby</h2>
<P></P>
Not many people like to read heaps of boring syntax rules when they're
picking up a new language. So we're going to cheat. In this section
we'll hit some of the highlights, the stuff you'll just <em>have</em> to
know if you're going to write Ruby programs. Later, in Chapter
18, which begins on page 201, we'll go into
all the gory details.
<P></P>
Let's start off with a simple Ruby program. We'll write a method that
returns a string, adding to that string a person's
name. We'll then invoke that method a couple of times.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def sayGoodnight(name)
result = "Goodnight, " + name
return result
end
<P></P>
# Time for bed...
puts sayGoodnight("John-Boy")
puts sayGoodnight("Mary-Ellen")
</pre></td></tr></table>
<P></P>
First, some general observations. Ruby syntax is clean. You don't
need semicolons at the ends of statements as long as you put each
statement on a separate line. Ruby comments start with a
<code>#</code> character and run to the end of the line. Code layout is
pretty much up to you; indentation is not significant.
<P></P>
Methods are defined with the keyword <code>def</code>, followed by the method
name (in this case, ``<code>sayGoodnight</code>'') and the method's
parameters between parentheses. Ruby doesn't use braces to delimit
the bodies of compound statements and definitions. Instead, you simply
finish the body with the keyword <code>end</code>. Our method's body is
pretty simple. The first line concatenates the literal string
``Goodnight,<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">'' to the parameter <code>name</code> and assigns the result to
the local variable result. The next line returns that result to the
caller. Note that we didn't have to declare the variable <code>result</code>;
it sprang into existence when we assigned to it.
<P></P>
Having defined the method, we call it twice. In both cases we pass the
result to the method <code>puts</code>, which simply outputs its argument
followed by a newline.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Goodnight, John-Boy
Goodnight, Mary-Ellen
</pre></td></tr></table>
<P></P>
The line ``<code>puts sayGoodnight("John-Boy")</code>'' contains two method calls,
one to <code>sayGoodnight</code> and the other to <code>puts</code>. Why does one call
have its arguments in parentheses while the other doesn't? In this
case it's purely a matter of taste. The following lines are all
equivalent.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
puts sayGoodnight "John-Boy"
puts sayGoodnight("John-Boy")
puts(sayGoodnight "John-Boy")
puts(sayGoodnight("John-Boy"))
</pre></td></tr></table>
<P></P>
However, life isn't always that simple, and precedence rules can make
it difficult to know which argument goes with which method invocation,
so we recommend using parentheses in all but the simplest cases.
<P></P>
This example also shows some Ruby string objects. There are many ways
to create a string object, but probably the most common is to use
string literals: sequences of characters between single or double
quotation marks. The difference between the two forms is the amount of
processing Ruby does on the string while constructing the literal. In
the single-quoted case, Ruby does very little. With a few exceptions,
what you type into the string literal becomes the string's value.
<P></P>
In the double-quoted case, Ruby does more work. First, it looks for
substitutions---sequences that start with a backslash character---and
replaces them with some binary value. The most common of these is
``<code>\n</code>'', which is replaced with a newline character.
When a
string containing a newline is output, the ``<code>\n</code>'' forces a
line break.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
puts "And Goodnight,\nGrandma"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
And Goodnight,
Grandma
</pre></td></tr></table>
<P></P>
The second thing that Ruby does with double-quoted strings
is expression interpolation. Within the string, the sequence
<code>#{</code><em>expression</em><code>}</code> is replaced by the value of
<em>expression</em>. We could use this to rewrite our previous method.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def sayGoodnight(name)
result = "Goodnight, #{name}"
return result
end
</pre></td></tr></table>
<P></P>
When Ruby constructs this string object, it looks at the current value
of <code>name</code> and substitutes it into the string. Arbitrarily complex
expressions are allowed in the <code>#{...}</code> construct. As a
shortcut, you don't need to supply the braces when the expression is
simply a global, instance, or class variable. For more
information on strings, as well as on the other Ruby standard types, see
Chapter 5, which begins on page 49.
<P></P>
Finally, we could simplify this method some more. The value returned
by a Ruby method is the value of the last expression evaluated, so we
can get rid of the <code>return</code> statement altogether.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def sayGoodnight(name)
"Goodnight, #{name}"
end
</pre></td></tr></table>
<P></P>
We promised that this section would be brief. We've got just one more
topic to cover: Ruby names. For brevity, we'll be using some terms
(such as class variable) that we aren't going to
define here. However, by talking about the rules now, you'll be ahead
of the game when we actually come to discuss instance variables and
the like later.
<P></P>
Ruby uses a convention to help it distinguish the usage of a name: the
first characters of a name indicate how the name is used.
Local variables, method parameters, and method names should all start
with a lowercase letter or with an underscore. Global variables are
prefixed with a dollar sign ($), while instance variables begin with
an ``at'' sign (@). Class variables start with two ``at'' signs (@@). Finally,
class names, module names, and constants should
start with an uppercase letter. Samples of different names are given
in Table 2.1 on page 10.
<P></P>
Following this initial character, a name can be any combination of
letters, digits, and underscores (with the proviso that the character
following an @ sign may not be a digit).
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>Example variable and class names</b>
<center>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td colspan="4" valign="top"><b>Variables</b></td>
<td valign="top"><b>Constants and</b></td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top"><b>Local</b></td>
<td valign="top"><b>Global</b></td>
<td valign="top"><b>Instance</b></td>
<td valign="top"><b>Class</b></td>
<td valign="top"><b>Class Names</b></td>
</tr>
<tr>
<td valign="top"><code>name</code></td>
<td valign="top"><code>$debug</code></td>
<td valign="top"><code>@name</code></td>
<td valign="top"><code>@@total</code></td>
<td valign="top"><code>PI</code></td>
</tr>
<tr>
<td valign="top"><code>fishAndChips</code></td>
<td valign="top"><code>$CUSTOMER</code></td>
<td valign="top"><code>@point_1</code></td>
<td valign="top"><code>@@symtab</code></td>
<td valign="top"><code>FeetPerMile</code></td>
</tr>
<tr>
<td valign="top"><code>x_axis</code></td>
<td valign="top"><code>$_</code></td>
<td valign="top"><code>@X</code></td>
<td valign="top"><code>@@N</code></td>
<td valign="top"><code>String</code></td>
</tr>
<tr>
<td valign="top"><code>thx1138</code></td>
<td valign="top"><code>$plan9</code></td>
<td valign="top"><code>@_</code></td>
<td valign="top"><code>@@x_pos</code></td>
<td valign="top"><code>MyClass</code></td>
</tr>
<tr>
<td valign="top"><code>_26</code></td>
<td valign="top"><code>$Global</code></td>
<td valign="top"><code>@plan9</code></td>
<td valign="top"><code>@@SINGLE</code></td>
<td valign="top"><code>Jazz_Song</code></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</center>
</td></tr></table>
<P></P>
<h2>Arrays and Hashes</h2>
<P></P>
Ruby's arrays and hashes are indexed collections. Both store
collections of objects, accessible using a key. With arrays, the key
is an integer, whereas hashes support any object as a key. Both
arrays and hashes grow as needed to hold new elements. It's more
efficient to access array elements, but hashes provide more
flexibility. Any particular array or hash can hold objects of
differing types; you can have an array containing an integer, a
string, and a floating point number, as we'll see in a minute.
<P></P>
You can create and initialize a new array using an array literal---a
set of elements between square brackets. Given an array object, you
can access individual elements by supplying an index between
square brackets, as the next example shows.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = [ 1, 'cat', 3.14 ] # array with three elements</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code># access the first element</code></td>
</tr>
<tr>
<td valign="top"><code>a[0]</code></td>
<td valign="top"></td>
<td valign="top"><code>1</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code># set the third element</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>a[2] = nil</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code># dump out the array</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>[1, "cat", nil]</code></td>
</tr>
</table>
<P></P>
<P></P>
You can create empty arrays either by using an array literal with no
elements or by using the array object's constructor, <a href="ref_c_array.html#new"><code>Array::new</code></a>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
empty1 = []
empty2 = Array.new
</pre></td></tr></table>
<P></P>
Sometimes creating arrays of words can be a pain, what with all the
quotes and commas. Fortunately, there's a shortcut: <code>%w</code> does just
what we want.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = %w{ ant bee cat dog elk }</code></td>
</tr>
<tr>
<td valign="top"><code>a[0]</code></td>
<td valign="top"></td>
<td valign="top"><code>"ant"</code></td>
</tr>
<tr>
<td valign="top"><code>a[3]</code></td>
<td valign="top"></td>
<td valign="top"><code>"dog"</code></td>
</tr>
</table>
<P></P>
<P></P>
Ruby hashes are similar to arrays. A hash literal uses braces rather than
square brackets. The literal must supply two objects for every
entry: one for the key, the other for the value.
<P></P>
For example, you might want to map musical instruments to their
orchestral sections. You could do this with a hash.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
instSection = {
'cello' => 'string',
'clarinet' => 'woodwind',
'drum' => 'percussion',
'oboe' => 'woodwind',
'trumpet' => 'brass',
'violin' => 'string'
}
</pre></td></tr></table>
<P></P>
Hashes are indexed using the same square bracket notation as arrays.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>instSection['oboe']</code></td>
<td valign="top"></td>
<td valign="top"><code>"woodwind"</code></td>
</tr>
<tr>
<td valign="top"><code>instSection['cello']</code></td>
<td valign="top"></td>
<td valign="top"><code>"string"</code></td>
</tr>
<tr>
<td valign="top"><code>instSection['bassoon']</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
</table>
<P></P>
<P></P>
As the last example shows, a hash by default returns <code>nil</code> when
indexed by a key it doesn't contain. Normally this is convenient, as
<code>nil</code> means <code>false</code> when used in conditional expressions.
Sometimes you'll want to change this default. For example, if you're
using a hash to count the number of times each key occurs, it's
convenient to have the default value be zero. This is easily done by
specifying a default value when you create a new, empty
hash.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>histogram = Hash.new(0)</code></td>
</tr>
<tr>
<td valign="top"><code>histogram['key1']</code></td>
<td valign="top"></td>
<td valign="top"><code>0</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>histogram['key1'] = histogram['key1'] + 1</code></td>
</tr>
<tr>
<td valign="top"><code>histogram['key1']</code></td>
<td valign="top"></td>
<td valign="top"><code>1</code></td>
</tr>
</table>
<P></P>
<P></P>
Array and hash objects have lots of useful methods: see the discussion
starting on page 35, and the reference sections starting on
pages 282 and 321, for details.
<h2>Control Structures</h2>
<P></P>
Ruby has all the usual control structures, such as <code>if</code> statements
and <code>while</code> loops. Java, C, and Perl programmers may well get
caught by the lack of braces around the bodies of these
statements. Instead, Ruby uses the keyword <code>end</code> to signify the end
of a body.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
if count > 10
puts "Try again"
elsif tries == 3
puts "You lose"
else
puts "Enter a number"
end
</pre></td></tr></table>
<P></P>
Similarly, <code>while</code> statements are terminated with <code>end</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
while weight < 100 and numPallets <= 30
pallet = nextPallet()
weight += pallet.weight
numPallets += 1
end
</pre></td></tr></table>
<P></P>
Ruby <em>statement modifiers</em> are a useful shortcut if the body of an
<code>if</code> or <code>while</code> statement is just a single expression. Simply
write the expression, followed by <code>if</code> or <code>while</code> and the
condition.
For example, here's a simple <code>if</code> statement.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
if radiation > 3000
puts "Danger, Will Robinson"
end
</pre></td></tr></table>
<P></P>
Here it is again, rewritten using a statement modifier.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
puts "Danger, Will Robinson" if radiation > 3000
</pre></td></tr></table>
<P></P>
Similarly, a <code>while</code> loop such as
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
while square < 1000
square = square*square
end
</pre></td></tr></table>
<P></P>
becomes the more concise
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
square = square*square while square < 1000
</pre></td></tr></table>
<P></P>
These statement modifiers should seem familiar to Perl programmers.
<h2>Regular Expressions</h2>
<P></P>
Most of Ruby's built-in types will be familiar to all programmers. A
majority of languages have strings, integers, floats, arrays, and so
on. However, until Ruby came along, regular expression support was
generally built into only the so-called scripting languages, such as
Perl, Python, and awk. This is a shame: regular expressions, although
cryptic, are a powerful tool for working with text.
<P></P>
Entire books have been written about regular expressions (for example,
<em>Mastering Regular Expressions</em> ), so we
won't try to cover everything in just a short section. Instead, we'll
look at just a few examples of regular expressions in action. You'll
find full coverage of regular expressions starting
on page 58.
<P></P>
A regular expression is simply a way of specifying a <em>pattern</em> of
characters to be matched in a string. In Ruby, you typically create a
regular expression by writing a pattern between slash characters
(/<em>pattern</em>/). And, Ruby being Ruby, regular expressions are of
course objects and can be manipulated as such.
<P></P>
For example, you could write a pattern that matches a string
containing the text ``Perl'' or the text ``Python'' using the
following regular expression.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
/Perl|Python/
</pre></td></tr></table>
<P></P>
The forward slashes delimit the pattern, which consists of the two
things we're matching, separated by a pipe character (``<code>|</code>'').
You can use parentheses within patterns, just as you can in arithmetic
expressions, so you could also have written this pattern as
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
/P(erl|ython)/
</pre></td></tr></table>
<P></P>
You can also specify repetition within patterns. <code>/ab+c/</code> matches a
string containing an ``a'' followed by one or more ``b''s, followed by
a ``c''. Change the plus to an asterisk, and <code>/ab*c/</code> creates a
regular expression that matches an ``a'', zero or more ``b''s, and a
``c''.
<P></P>
You can also match one of a group of characters within a pattern. Some
common examples are character classes such as ``<code>\s</code>'', which
matches a whitespace character (space, tab, newline, and so on),
``<code>\d</code>'', which matches any digit, and ``<code>\w</code>'', which matches
any character that may appear in a typical word. The single character
``.'' (a period) matches any character.
<P></P>
We can put all this together to produce some useful regular
expressions.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
/\d\d:\d\d:\d\d/ # a time such as 12:34:56
/Perl.*Python/ # Perl, zero or more other chars, then Python
/Perl\s+Python/ # Perl, one or more spaces, then Python
/Ruby (Perl|Python)/ # Ruby, a space, and either Perl or Python
</pre></td></tr></table>
<P></P>
Once you have created a pattern, it seems a shame not to use it. The
match operator ``<code>=~</code>'' can be used to match a string against a
regular expression. If the pattern is found in the string, <code>=~</code>
returns its starting position, otherwise it returns <code>nil</code>. This means
you can use regular expressions as the condition in <code>if</code> and
<code>while</code> statements. For example, the following code fragment writes
a message if a string contains the text 'Perl' or 'Python'.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
if line =~ /Perl|Python/
puts "Scripting language mentioned: #{line}"
end
</pre></td></tr></table>
<P></P>
The part of a string matched by a regular expression can also be
replaced with different text using one of Ruby's substitution methods.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
line.sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby'
line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby'
</pre></td></tr></table>
<P></P>
We'll have a lot more to say about regular expressions as we go
through the book.
<h2>Blocks and Iterators</h2>
<P></P>
This section briefly describes one of Ruby's particular strengths. We're
about to look at code blocks: chunks of code that you can associate
with method invocations, almost as if they were parameters. This is an
incredibly powerful feature. You can use code blocks to implement
callbacks (but they're simpler than Java's anonymous inner classes),
to pass around chunks of code (but they're more flexible than C's
function pointers), and to implement iterators.
<P></P>
Code blocks are just chunks of code between braces or
<code>do</code>...<code>end</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
{ puts "Hello" } # this is a block
<P></P>
do #
club.enroll(person) # and so is this
person.socialize #
end #
</pre></td></tr></table>
<P></P>
Once you've created a block, you can associate it with a call to a
method. That method can then invoke the block one or more times
using the Ruby <code>yield</code> statement. The following example shows this
in action. We define a method that calls <code>yield</code> twice. We then
call it, putting a block on the same line, after the call (and after
any arguments to the method).<em>[Some people like to think of
the association of a block with a method as a kind of parameter
passing. This works on one level, but it isn't really the whole
story. You might be better off thinking of the block and the method
as coroutines, which
transfer control back and forth between themselves.]</em>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def callBlock
yield
yield
end
<P></P>
callBlock { puts "In the block" }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
In the block
In the block
</pre></td></tr></table>
<P></P>
See how the code in the block (<code>puts "In the block"</code>) is executed
twice, once for each call to <code>yield</code>.
<P></P>
You can provide parameters to the call to
<code>yield</code>: these will be passed to the block. Within the block, you
list the names of the arguments to receive these parameters between
vertical bars (``|'').
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
def callBlock
yield ,
end
<P></P>
callBlock { |, | ... }
</pre></td></tr></table>
<P></P>
Code blocks are used throughout the Ruby library to implement
iterators: methods that return successive elements from some kind of
collection, such as an array.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
a = %w( ant bee cat dog elk ) # create an array
a.each { |animal| puts animal } # iterate over the contents
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
ant
bee
cat
dog
elk
</pre></td></tr></table>
<P></P>
Let's look at how we might implement the <code>Array</code> class's <code>each</code>
iterator that we used in the previous example. The <code>each</code>
iterator loops through every element in the array,
calling <code>yield</code> for each one. In pseudo code, this might look like:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
# within class Array...
def each
for each element
yield(element)
end
end
</pre></td></tr></table>
<P></P>
You could then iterate over an array's elements by calling its
<code>each</code> method and supplying a block. This block would be called for
each element in turn.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
[ 'cat', 'dog', 'horse' ].each do |animal|
print animal, " -- "
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
cat -- dog -- horse --
</pre></td></tr></table>
<P></P>
Similarly, many looping constructs that are built into languages such
as C and Java are simply method calls in Ruby, with the methods
invoking the associated block zero or more times.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
5.times { print "*" }
3.upto(6) {|i| print i }
('a'..'e').each {|char| print char }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
*****3456abcde
</pre></td></tr></table>
<P></P>
Here we ask the number 5 to call a block five times, then ask
the number 3 to call a block, passing in successive values until
it reaches 6. Finally, the range of characters from ``a'' to ``e''
invokes a block using the method <code>each</code>.
<h2>Reading and 'Riting</h2>
<P></P>
Ruby comes with a comprehensive I/O library. However, in most of the
examples in this book we'll stick to a few simple methods. We've
already come across two methods that do output. <code>puts</code> writes each
of its arguments, adding a newline after each. <code>print</code> also writes
its arguments, but with no newline. Both can be used to write to any
I/O object, but by default they write to the console.
<P></P>
Another output method we use a lot is <code>printf</code>, which prints
its arguments under the control of a format string (just like
<code>printf</code> in C or Perl).
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
printf "Number: %5.2f, String: %s", 1.23, "hello"
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Number: 1.23, String: hello
</pre></td></tr></table>
<P></P>
In this example, the format string <code>"Number: %5.2f, String: %s"</code>
tells <code>printf</code> to substitute in a floating point number
(allowing five characters in total, with two after the decimal point)
and a string.
<P></P>
There are many ways to read input into your program. Probably the most
traditional is to use the routine <code>gets</code>, which returns the next
line from your program's standard input stream.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
line = gets
print line
</pre></td></tr></table>
<P></P>
The <code>gets</code> routine has a side effect: as well as returning the line
just read, it also stores it into the global variable
<code>$_</code>. This variable is special, in that it is used as the
default argument in many circumstances. If you call <code>print</code> with no
argument, it prints the contents of <code>$_</code>. If you write an <code>if</code>
or <code>while</code> statement with just a regular expression as the
condition, that expression is matched against <code>$_</code>. While viewed
by some purists as a rebarbative barbarism, these abbreviations can
help you write some concise programs. For example, the following
program prints all lines in the input stream that contain the word
``Ruby.''
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
while gets # assigns line to $_
if /Ruby/ # matches against $_
print # prints $_
end
end
</pre></td></tr></table>
<P></P>
The ``Ruby way'' to write this would be to use an iterator.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
ARGF.each { |line| print line if line =~ /Ruby/ }
</pre></td></tr></table>
<P></P>
This uses the predefined object <code>ARGF</code>, which represents the
input stream that can be read by a program.
<h2>Onward and Upward</h2>
<P></P>
That's it. We've finished our lightning-fast tour of some of the
basic features of Ruby. We've had a brief look at objects, methods,
strings, containers, and regular expressions, seen some simple control
structures, and looked at some rather nifty iterators. Hopefully, this
chapter has given you enough ammunition to be able to attack the rest
of this book.
<P></P>
Time to move on, and up---up to a higher level. Next, we'll be looking
at classes and objects, things that are at the same time both the
highest-level constructs in Ruby and the essential underpinnings of
the entire language.
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="roadmap.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="tut_classes.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|