You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/documentation/faq/10/index.md
+32-13
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,8 @@ header: |
35
35
36
36
10.1 How can I use Ruby interactively?
37
37
38
-
You can try using irb. The following is paraphrased from Goto Kentaro (Gotoken), and originally appeared in ruby-talk:444.
38
+
You can try using irb. The following is paraphrased from Goto Kentaro
39
+
(Gotoken), and originally appeared in ruby-talk:444.
39
40
40
41
Get the latest tarball of irb from the contrib directory in the Ruby archive.
41
42
Extract the irb directory tree.
@@ -45,48 +46,66 @@ You can try using irb. The following is paraphrased from Goto Kentaro (Gotoken),
45
46
Possibly use rehash to tell your login shell about the new command.
46
47
Type irb
47
48
48
-
If the readline extension module works with your interpreter, it makes irb a lot more fun to use.
49
+
If the readline extension module works with your interpreter, it makes irb
50
+
a lot more fun to use.
51
+
52
+
There is also a simple program, eval, in the samples/ directory of the Ruby
53
+
distribution. It lets you enter expressions and view their values. You can
54
+
copy eval into the site_ruby directory in the Ruby tree, and then invoke it
55
+
using:
49
56
50
-
There is also a simple program, eval, in the samples/ directory of the Ruby distribution. It lets you enter expressions and view their values. You can copy eval into the site_ruby directory in the Ruby tree, and then invoke it using:
51
57
52
-
53
58
54
59
ruby -r eval -e0
55
60
56
61
10.2 Is there a debugger for Ruby?
57
62
58
63
There is a gdb-like debugger for Ruby.
59
64
60
-
65
+
61
66
62
67
ruby -r debug your_program
63
68
64
69
10.3 How can I use a library written in C from Ruby?
65
70
66
-
Of all the scripting languages, Ruby is probably the easiest to extend. There are no problems with reference counting and variable types, and very few interfaces to learn. In fact, C code used to extend Ruby often ends up looking surprisingly like Ruby code itself.
71
+
Of all the scripting languages, Ruby is probably the easiest to extend.
72
+
There are no problems with reference counting and variable types, and very
73
+
few interfaces to learn. In fact, C code used to extend Ruby often ends up
74
+
looking surprisingly like Ruby code itself.
67
75
68
-
First, get the Ruby source distribution and read README.EXT. This is a good document, not only if you're writing an extension library, but also if you want to understand Ruby more deeply.
76
+
First, get the Ruby source distribution and read README.EXT. This is a good
77
+
document, not only if you're writing an extension library, but also if you
78
+
want to understand Ruby more deeply.
69
79
70
-
Next, have a look at the source of the interpreter itself, and at the various supplied extensions in the ext/ directory. You'll also find good examples under contrib/ on the Ruby ftp sites.
80
+
Next, have a look at the source of the interpreter itself, and at the various
81
+
supplied extensions in the ext/ directory. You'll also find good examples
82
+
under contrib/ on the Ruby ftp sites.
71
83
72
84
10.4 Can I use Tcl/Tk interface in Ruby?
73
85
74
-
There are two interfaces to Tcl/Tk included in the standard distribution. One is under ext/tcltk/ and loaded with require "tcltk". The syntax is very close to that Tcl, which is passed to Tcl interpreter. Unfortunately, the description for this library is written in Japanese.
86
+
There are two interfaces to Tcl/Tk included in the standard distribution.
87
+
One is under ext/tcltk/ and loaded with require "tcltk". The syntax is very
88
+
close to that Tcl, which is passed to Tcl interpreter. Unfortunately, the
89
+
description for this library is written in Japanese.
75
90
76
-
The other is under ext/tk/ and loaded with require "tk". Its syntax closer to the style of the Tk interface provided by the Perl and Python interfaces.
91
+
The other is under ext/tk/ and loaded with require "tk". Its syntax closer to
92
+
the style of the Tk interface provided by the Perl and Python interfaces.
77
93
78
94
10.5 Tk won't work.
79
95
80
96
Your Tk version may be old, try a newer version.
81
97
82
98
10.6 Can I use gtk+ or xforms interfaces in Ruby?
83
99
84
-
You'll find ruby-gtk-x.xx.tar.gz and ruby-forms-x.x.tar.gz under contrib/ in ftp sites.
100
+
You'll find ruby-gtk-x.xx.tar.gz and ruby-forms-x.x.tar.gz under contrib/
101
+
in ftp sites.
85
102
86
103
10.7 How can I do date arithmetic?
87
104
88
105
A Time object can express only the dates between Jan 1, 1970 and Jan 19, 2038.
89
106
90
-
Two standard extension library modules are provided: require "date", which is simple and uses the English calendar, and require "date2", which is more general purpose.
107
+
Two standard extension library modules are provided:
108
+
require "date", which is simple and uses the English calendar,
109
+
and require "date2", which is more general purpose.
Copy file name to clipboardExpand all lines: en/documentation/faq/11/index.md
+29-17
Original file line number
Diff line number
Diff line change
@@ -39,21 +39,26 @@ It's the same as saying if a then b else c end.
39
39
40
40
11.2 How can I count the number of lines in a file?
41
41
42
-
Assuming that the file ends in a linefeed, the following code may give the fastest result.
42
+
Assuming that the file ends in a linefeed, the following code may give the
43
+
fastest result.
44
+
43
45
44
-
45
46
46
47
open("example").read.count("\n") # -> 3
47
48
48
49
11.3 What do begin and end of MatchingData return?
49
50
50
-
They act with $ , and return the start index and the end index of the matched data ($0) in the original string. See an example in tab expansion.
51
+
They act with $ , and return the start index and the end index of the
52
+
matched data ($0) in the original string. See an example in tab expansion.
51
53
52
54
11.4 How can I sum the elements in an array?
53
55
54
-
Rather than solve the specific problem, let's solve the general case. The first thing we'll do is produce a method that will iterate over an Enumerable object and collect a single result. Smalltalk calls that method inject, so we will too:
56
+
Rather than solve the specific problem, let's solve the general case. The
57
+
first thing we'll do is produce a method that will iterate over an Enumerable
58
+
object and collect a single result. Smalltalk calls that method inject, so we
59
+
will too:
60
+
55
61
56
-
57
62
58
63
module Enumerable
59
64
@@ -66,9 +71,14 @@ module Enumerable
66
71
end
67
72
end
68
73
69
-
Notice how we've added the method to Enumerable. This means that anything that includes Enumerable can now use inject. But how do we use it? It takes a single argument `n' and a block. For each element in the thing being enumerated, it calls the block, passing in `n' and the element itself. The result of the block is assigned back to `n'. So, to define sum, we could write:
74
+
Notice how we've added the method to Enumerable. This means that anything
75
+
that includes Enumerable can now use inject. But how do we use it? It takes
76
+
a single argument `n' and a block. For each element in the thing being
77
+
enumerated, it calls the block, passing in `n' and the element itself.
78
+
The result of the block is assigned back to `n'. So, to define sum,
79
+
we could write:
80
+
70
81
71
-
72
82
73
83
module Enumerable
74
84
def sum
@@ -81,11 +91,15 @@ end
81
91
82
92
11.5 How can I use continuations?
83
93
84
-
Ruby's continuations allow you to create an object representing a place in a Ruby program, and then return to that place at any time (even if it has apparently gone out of scope). Continuations can be used to implement complex control structures, but are typically more useful as ways of confusing people.
94
+
Ruby's continuations allow you to create an object representing a place in a
95
+
Ruby program, and then return to that place at any time (even if it has
96
+
apparently gone out of scope). Continuations can be used to implement complex
97
+
control structures, but are typically more useful as ways of confusing people.
98
+
99
+
In [ruby-talk:4482], Jim Weirich posted the following examples of
100
+
continuations:
85
101
86
-
In [ruby-talk:4482], Jim Weirich posted the following examples of continuations:
Copy file name to clipboardExpand all lines: en/documentation/faq/2/index.md
+53-9
Original file line number
Diff line number
Diff line change
@@ -30,24 +30,68 @@ header: |
30
30
<h1>Official Ruby FAQ</h1>
31
31
32
32
---
33
+
33
34
## How Does Ruby Stack Up Against...?
34
35
35
36
### How Does Ruby Compare With Python?
36
37
37
-
Python and Ruby are both object oriented languages that provide a smooth transition from procedural to OO programming styles. Smalltalk, by contrast, is object only - you can't do anything until you understand objects, inheritance and the sizable Smalltalk class hierarchy. By providing procedural training wheels, Python and Ruby ``fix'' one of the features that may have kept Smalltalk out of the mainstream. The two languages differ by approaching this solution from opposite directions.
38
+
Python and Ruby are both object oriented languages that provide a smooth
39
+
transition from procedural to OO programming styles. Smalltalk, by contrast,
40
+
is object only - you can't do anything until you understand objects,
41
+
inheritance and the sizable Smalltalk class hierarchy. By providing procedural
42
+
training wheels, Python and Ruby ``fix'' one of the features that may have
43
+
kept Smalltalk out of the mainstream. The two languages differ by approaching
44
+
this solution from opposite directions.
38
45
39
-
Python is a hybrid language. It has functions for procedural programming and objects for OO programming. Python bridges the two worlds by allowing functions and methods to interconvert using the explicit ``self'' parameter of every method def. When a function is inserted into an object, the first argument automagically becomes a reference to the receiver.
46
+
Python is a hybrid language. It has functions for procedural programming and
47
+
objects for OO programming. Python bridges the two worlds by allowing
48
+
functions and methods to interconvert using the explicit ``self'' parameter
49
+
of every method def. When a function is inserted into an object, the first
50
+
argument automagically becomes a reference to the receiver.
40
51
41
-
Ruby is a pure OO language that can masquerade as a procedural one. It has no functions, only method calls. In a Ruby method the receiver, also called self, is a hidden argument like ``this'' in C++. A ``def'' statement outside of a class definition, which is a function in Python, is actually a method call in Ruby. These ersatz functions become private methods of class Object, the root of the Ruby class hierarchy. Procedural programming is neatly solved from the other direction - everything is an object. If the user doesn't grok objects yet, they can just pretend that ``def'' is a function definition and still get useful work done.
52
+
Ruby is a pure OO language that can masquerade as a procedural one. It has no
53
+
functions, only method calls. In a Ruby method the receiver, also called self,
54
+
is a hidden argument like ``this'' in C++. A ``def'' statement outside of a
55
+
class definition, which is a function in Python, is actually a method call in
56
+
Ruby. These ersatz functions become private methods of class Object, the root
57
+
of the Ruby class hierarchy. Procedural programming is neatly solved from the
58
+
other direction - everything is an object. If the user doesn't grok objects
59
+
yet, they can just pretend that ``def'' is a function definition and still
60
+
get useful work done.
42
61
43
-
Ruby's OO purity provides a number features that Python lacks or is still working toward: a unified type/class hierarchy, metaclasses, the ability to subclass everything, and uniform method invocation (none of this len() is a function but items() is a method rubbish). Ruby, like Smalltalk, only supports single inheritance, but it does have a very powerful mixin concept: a class definition may include a module, which inserts that module's methods, constants, etc. into the class.
62
+
Ruby's OO purity provides a number features that Python lacks or is still
63
+
working toward: a unified type/class hierarchy, metaclasses, the ability to
64
+
subclass everything, and uniform method invocation (none of this len() is a
65
+
function but items() is a method rubbish). Ruby, like Smalltalk, only supports
66
+
single inheritance, but it does have a very powerful mixin concept: a class
67
+
definition may include a module, which inserts that module's methods,
68
+
constants, etc. into the class.
44
69
45
-
Ruby, again like Smalltalk, provides closures and code blocks and uses them to the same good effect. The Ruby collection classes and iterators are outstanding, much more powerful and elegant than the ad hoc solutions that Python is sprouting (lambdas and list comprehensions).
70
+
Ruby, again like Smalltalk, provides closures and code blocks and uses them
71
+
to the same good effect. The Ruby collection classes and iterators are
72
+
outstanding, much more powerful and elegant than the ad hoc solutions that
73
+
Python is sprouting (lambdas and list comprehensions).
46
74
47
-
Ruby's syntax and design philosophy are heavily influenced by Perl. It has a lot of syntactic variability. Statement modifiers (if, unless, while, until, etc.) may appear at the end of any statement. Some key words are optional (the ``then'' in an ``if'' statement for example). Parentheses may sometimes be elided in method calls. The receiver of a method may usually be elided. Many, many things are lifted directly from Perl. Built in regular expressions, $_ and friends, here documents, the single-quoted / double-quoted string distinction, $ and @ prefixes to distinguish different kinds of names and so forth.
75
+
Ruby's syntax and design philosophy are heavily influenced by Perl. It has a
76
+
lot of syntactic variability. Statement modifiers (if, unless, while, until,
77
+
etc.) may appear at the end of any statement. Some key words are optional
78
+
(the ``then'' in an ``if'' statement for example). Parentheses may sometimes
79
+
be elided in method calls. The receiver of a method may usually be elided.
80
+
Many, many things are lifted directly from Perl.
81
+
Built in regular expressions, $_ and friends, here documents,
82
+
the single-quoted / double-quoted string distinction, $ and @ prefixes
83
+
to distinguish different kinds of names and so forth.
48
84
49
-
If you like Perl, you will like Ruby and be right at home with its syntax. If you like Smalltalk, you will like Ruby and be right at home with its semantics. If you like Python, you may or may not be put off by the huge difference in design philosophy between Python and Ruby/Perl.
85
+
If you like Perl, you will like Ruby and be right at home with its syntax.
86
+
If you like Smalltalk, you will like Ruby and be right at home with its
87
+
semantics. If you like Python, you may or may not be put off by the huge
88
+
difference in design philosophy between Python and Ruby/Perl.
50
89
51
-
Ruby is much more complex than Python but its features, for the most part, hang together well. Ruby is well designed and full of neat ideas that might be mined for P3K. I'm not sure how many Python programmers will be attracted to it though - it hasn't won me over (yet). But it is worthy of serious study and could be a real threat to Perl.
90
+
Ruby is much more complex than Python but its features, for the most part,
91
+
hang together well. Ruby is well designed and full of neat ideas that might be
92
+
mined for P3K. I'm not sure how many Python programmers will be attracted to
93
+
it though - it hasn't won me over (yet). But it is worthy of serious study and
94
+
could be a real threat to Perl.
52
95
53
-
Posted by John Dell'Aquila in comp.lang.python, 11/17/2000. Reproduced with permission.
96
+
Posted by John Dell'Aquila in comp.lang.python, 11/17/2000. Reproduced with
0 commit comments