Skip to content

Commit 7353358

Browse files
Zachary Scottstomar
Zachary Scott
authored andcommitted
Import faq chapter 1
1 parent 548b91e commit 7353358

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

en/documentation/faq/index.md

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
---
2+
layout: page
3+
title: "Official Ruby FAQ"
4+
lang: en
5+
6+
header: |
7+
<div class="multi-page">
8+
<strong>1</strong>
9+
<span class="separator"> | </span>
10+
<a href="2/" title="Part 2">2</a>
11+
<span class="separator"> | </span>
12+
<a href="3/" title="Part 3">3</a>
13+
<span class="separator"> | </span>
14+
<a href="4/" title="Part 4">4</a>
15+
<span class="separator"> | </span>
16+
<a href="5/" title="Part 5">5</a>
17+
<span class="separator"> | </span>
18+
<a href="6/" title="Part 6">6</a>
19+
<span class="separator"> | </span>
20+
<a href="7/" title="Part 7">7</a>
21+
<span class="separator"> | </span>
22+
<a href="8/" title="Part 8">8</a>
23+
<span class="separator"> | </span>
24+
<a href="9/" title="Part 9">9</a>
25+
<span class="separator"> | </span>
26+
<a href="10/" title="Part 10">10</a>
27+
<span class="separator"> | </span>
28+
<a href="11/" title="Part 11">11</a>
29+
</div>
30+
<h1>Official Ruby FAQ</h1>
31+
32+
---
33+
34+
# General questions
35+
36+
## What is Ruby?
37+
38+
Ruby is a simple and powerful object-oriented programming language, created by Yukihiro Matsumoto (who goes by the handle "matz" in this document and on the mailing lists).
39+
40+
Like Perl, Ruby is good at text processing. Like Smalltalk, everything in Ruby is an object, and Ruby has blocks, iterators, meta-classes and other good stuff.
41+
42+
You can use Ruby to write servers, experiment with prototypes, and for everyday programming tasks. As a fully-integrated object-oriented language, Ruby scales well.
43+
44+
Ruby features:
45+
46+
* Simple syntax,
47+
* Basic OO features (classes, methods, objects, and so on),
48+
* Special OO features (Mix-ins, singleton methods, renaming, ...),
49+
* Operator overloading,
50+
* Exception handling,
51+
* Iterators and closures,
52+
* Garbage collection,
53+
* Dynamic loading (depending on the architecture),
54+
* High transportability (runs on various Unices, Windows, DOS, OSX, OS/2, Amiga, and so on)
55+
56+
## Show me some Ruby code.
57+
58+
Let's define a class called Person, with a name and an age. We'll test our code by creating a few people and examining them.
59+
60+
class Person
61+
attr_accessor :name, :age
62+
def initialize(name, age)
63+
@name = name
64+
@age = age.to_i
65+
end
66+
def inspect
67+
"#@name (#@age)"
68+
end
69+
end
70+
71+
p1 = Person.new('elmo', 4)
72+
p2 = Person.new('zoe', 7)
73+
74+
p1 # -> elmo (4)
75+
p2 # -> zoe (7)
76+
77+
Now let's populate an array of people by reading their names and ages from a file containing lines like:
78+
79+
bert: 8
80+
cookie: 11
81+
elmo: 4
82+
ernie: 8
83+
zoe: 7
84+
85+
The code uses regular expressions to parse successive lines from the input file, creating a new Person object for each match and pushing it on to the end of the array people.
86+
87+
people = Array.new
88+
89+
File.foreach("ages") { |l|
90+
people << Person.new($1, $2) if l =~ /(.*):\s+(\d+)/
91+
}
92+
93+
people # -> [bert (8), cookie (11), elmo (4), ernie (8), zoe (7)]
94+
95+
Now, let's sort the result based on the person's age. There are many ways to do this. We can define a sort block, which tells Ruby how to do the comparison of two people:
96+
97+
sorted = people.sort do |a,b| a.age <=> b.age end
98+
sorted # -> [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
99+
100+
Another way would be to change the comparison method for class Person:
101+
102+
class Person
103+
def <=>(other)
104+
@age <=> other.age
105+
end
106+
end
107+
people.sort # -> [elmo (4), zoe (7), bert (8), ernie (8), cookie (11)]
108+
109+
## Why the name 'Ruby'?
110+
111+
Influenced by Perl, Matz wanted to use a jewel name for his new language, so he named Ruby after a colleague's birthstone.
112+
113+
Later, he realized that Ruby comes right after Perl in several situations. In birthstones, pearl is June, ruby is July. When measuring font sizes, pearl is 5pt, ruby is 5.5pt. He thought Ruby was a good name for a programming language newer (and hopefully better) than Perl.
114+
115+
(Based on an explanation from matz in [ruby-talk:00394] on June 11, 1999.)
116+
117+
## What is the history of Ruby?
118+
119+
The following a summary of a posting made by Matz in [ruby-talk:00382] on June 4, 1999. (The birthday of Ruby is corrected in [ruby-list:15977]).
120+
121+
> Well, Ruby was born on February 24 1993. I was talking with my colleague about the possibility of an object-oriented scripting language. I knew Perl (Perl4, not Perl5), but I didn't like it really, because it had smell of toy language (it still has). The object-oriented scripting language seemed very promising.
122+
123+
> I knew Python then. But I didn't like it, because I didn't think it was a true object-oriented language---OO features appeared to be add-on to the language. As a language manic and OO fan for 15 years, I really wanted a genuine object-oriented, easy-to-use scripting language. I looked for, but couldn't find one.
124+
125+
> So, I decided to make it. It took several months to make the interpreter run. I put it the features I love to have in my language, such as iterators, exception handling, garbage collection.
126+
127+
> Then, I reorganized the features of Perl into a class library, and implemented them. I posted Ruby 0.95 to the Japanese domestic newsgroups in Dec. 1995.
128+
129+
> Since then, highly active mailing lists have been established and web pages formed.
130+
131+
## Where is the Ruby Home Page?
132+
133+
The official Ruby Home Page is http://www.ruby-lang.org (in English) and http://www.ruby-lang.org/ja/ (in Japanese).
134+
135+
You can also find Ruby information at http://www.rubycentral.com. In particular, there is a complete online reference to Ruby's built-in classes and methods.
136+
137+
## Is there a Ruby newsgroup?
138+
139+
comp.lang.ruby was established in May, 2000 (thanks to the efforts of Conrad Schneiker).
140+
141+
## Is there a Ruby mailing list?
142+
143+
There are five mailing lists now talking about Ruby. The first is in English, the last four in Japanese:
144+
145+
ruby-talk: English language discussion of Ruby.
146+
ruby-list: Japanese language discussion of Ruby.
147+
ruby-dev: List for Ruby developers.
148+
ruby-ext: List for people writing extensions for or with Ruby.
149+
ruby-math: Ruby in mathematics.
150+
151+
See joining the mailing list.
152+
153+
You can search the mailing list archives using http://blade.nagaokaut.ac.jp/ruby/ruby-talk/index.shtml. (This is the url for the ruby-talk list: munge as required for the others).
154+
155+
1.8 **Changed** How can I thread the mailing list in mutt?
156+
157+
The Ruby mailing list software adds a prefix to the subject lines, for example [ruby-talk:1234]. This can confuse the threading in some mail user agents.
158+
159+
In mutt, you can get threading to work using the following variable setting.
160+
161+
# reply regexp, to support MLs like ruby-talk.
162+
set reply_regexp="^(\[[a-z0-9:-]+\][[:space:]]*)?(re([\[0-9\]+])*|aw):[[:space:]]*"
163+
164+
## Which is correct, Ruby or ruby?
165+
166+
Officially, the language is called ``Ruby''. On most systems, it is invoked using the command ``ruby''. It's OK to use ruby instead of Ruby.
167+
168+
Please don't use RUBY as the language name.
169+
170+
Originally, or historically, it was called ``ruby''.
171+
172+
## Are there any Ruby books?
173+
174+
Programming Ruby: The Pragmatic Programmer's Guide, (the Pickaxe Book) by David Thomas and Andrew Hunt: ISBN 0-20171-089-7, Addison-Wesley, October 2000.
175+
176+
A Japanese language Ruby reference book by matz, et al and published by ASCII is available in Japan (ISBN 4-7561-3254-5). An English translation, `` The Ruby Programming Language,'' is in the works from Addison-Wesley (ISBN 020171096X).
177+
178+
A Japanese language ``Ruby Pocket Reference'' is published by O'Reilly Japan (ISBN 4-87311-023-8). Let O'Reilly in the US know if you'd like to see a translation.
179+
180+
In addition, `` Mastering Regular Expressions,'' by Jeffrey Friedl, (the Hip Owl Book): ISBN 1-56592-257-3 from O'Reilly & Associates, is a reference work that covers the art and implementation of regular expressions in various programming languages. Most of it is highly relevant to Ruby regular expressions.
181+
182+
## Which editors provide support for Ruby.
183+
184+
* Emacs or XEmacs: ruby-mode.el is supplied in the Ruby distribution. With some versions of XEmacs, you may need to add (load "font-lock") to your .emacs file to allow ruby-mode.el to detect the syntax highlighting package you're using.
185+
* Vim: Vim 5.7 and later have Ruby syntax files as standard in the runtime package. For prior versions, a syntax file for Ruby is available at http://www.xs4all.nl/~hipster/lib/ruby/ruby.vim.
186+
* Jedit: A portable editor written in Java, comes with support for Ruby.
187+
* Jed: An s-lang file supporting Ruby is available at http://www.kondara.org/~g/slang/ruby.sl.
188+
* Nedit ( http://www.nedit.org): Eric Santonacci has written Ruby support for Nedit, available from ftp://ftp.talc.fr/pub/ruby/ruby.nedit-0.1.tar.gz.
189+
* Barry Shultz has written a Ruby definition file for TextPad, available at http://www.textpad.com/add-ons/ntsyn.html.
190+
191+
## How can I annotate Ruby code with its results?
192+
193+
People commonly annotate Ruby code by showing the results of executing each statement as a comment attached to that statement. For example, in the following code, we show that the assignment generates the string "Billy Bob", and then result of extracting some substrings.
194+
195+
str = "Billy" + " Bob" # -> "Billy Bob"
196+
str[0,1] + str[2,1] + str[-2,2] # -> "Blob"
197+
198+
Gotoken's xmp package, available from http://www.ruby-lang.org/en/raa-list.rhtml?name=xmp is a utility that annotates Ruby source code this way.
199+
200+
Emacs and vim users can integrate this with their editing environments, which is useful if you want to send people e-mail with annotated Ruby code. Having installed xmp, Emacs users can add the following to their .emacs file:
201+
202+
(defun ruby-xmp-region (reg-start reg-end)
203+
"Pipe the region through Ruby's xmp utility and replace the region with
204+
the result."
205+
(interactive "r")
206+
(shell-command-on-region reg-start reg-end
207+
"ruby -r xmp -n -e 'xmp($_, \"%l\t\t# %r\n\")'"
208+
t))
209+
(global-set-key [(meta f10)] 'ruby-xmp-region)
210+
211+
Vim users can use the mapping (thanks to hipster):
212+
213+
map <M-F10> :!ruby -r xmp -n -e 'xmp($_, "\%l\t\t\# \%r\n")'<CR>
214+
215+
In both cases, highlight a region of code and hit Meta-F10 to annotate it.
216+
217+
## I can't understand Ruby even after reading the manual!
218+
219+
The syntax of Ruby has been fairly stable since Ruby 1.0, but new features are added every now and then. So, the books and the online documentation can get behind.
220+
221+
If you have a problem, feel free to ask in the mailing list (see ruby-talk mailing list). Generally you'll get timely answers from matz himself, the author of the language, from other gurus, and from those who've solved problems similar to your own.
222+
223+
Please include the output of ruby -v along with any problematic source code.
224+
225+
If you have a problem using irb, be aware that it has some limitations. Try the script using `irb --single-irb`, or directly using the ruby command.
226+
227+
There might be similar questions in the mailing list, and it is good netiquette to read through recent mails (RFC1855:3.1.1, 3.1.2) before asking. But do ask on the list, and a correct answer will be forthcoming.
228+

en/documentation/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ of getting Ruby.
3030
browser. This 15-minute tutorial is aimed at beginners who want to get
3131
a feeling of the language.
3232

33+
[Official FAQ](/en/documentation/faq/)
34+
: The offical frequently asked questions.
35+
3336
[Ruby Koans][2]
3437
: The Koans walk you along the path to enlightenment in order to learn
3538
Ruby. The goal is to learn the Ruby language, syntax, structure, and

0 commit comments

Comments
 (0)