@@ -9,28 +9,128 @@ definition:
99
1010A method definition consists of the +def+ keyword, a method name, the body of
1111the method, then the +end+ keyword. When called the method will execute the
12- body of the method. This method returns <tt>2</tt> .
12+ body of the method. This method returns +2+ .
1313
14- A method may be defined on another object. You may define a "class
15- method" (a method that is called on the class, not an instance of the class)
16- like this:
14+ == Return values
15+
16+ By default, a method returns the last expression that was evaluated in the body
17+ of the method. In the example above, the last (and only) expression evaluated
18+ was the simple sum <code>1 + 1</code>. The +return+ keyword can be used to
19+ make it explicit that a method returns a value.
20+
21+ def one_plus_one
22+ return 1 + 1
23+ end
24+
25+ It can also be used to make a method return before the last expression is
26+ evaluated.
27+
28+ def two_plus_two
29+ return 2 + 2
30+ 1 + 1 # this expression is never evaluated
31+ end
32+
33+ == Scope
34+
35+ The standard syntax to define a method:
36+
37+ def my_method
38+ # ...
39+ end
40+
41+ add the method to a class. You can define an instance method on a specific
42+ class with the +class+ keyword:
43+
44+ class C
45+ def my_method
46+ # ...
47+ end
48+ end
49+
50+ In many languages, the +class+ keyword lets the compiler know that you're
51+ creating a class. This is true in Ruby, too, the first time you use the
52+ _class_ keyword: when it sees that you're _opening_ a class for
53+ the first time, it creates it. When you open a class that already exists, Ruby
54+ enables to you _extend_ it with new methods. You can even extend core
55+ classes:
56+
57+ class String
58+ def hello
59+ "Hello, world!"
60+ end
61+ end
62+
63+ "".hello # returns "Hello, world!"
64+
65+ However, This is somewhat risky due to namespace pollution so this ability is
66+ best used sparingly.
67+
68+ A method may be defined on another object. You may define a "class method" (a
69+ method that is defined on the class, not an instance of the class) like this:
1770
1871 class C
1972 def self.my_method
2073 # ...
2174 end
2275 end
2376
24- You may also define methods this way on any object :
77+ or a more concrete example :
2578
26- string = "my string"
27- def string.my_method
28- # ...
79+ class String
80+ def self.hello
81+ "Hello, world!"
82+ end
2983 end
3084
31- This is called a "singleton method". +my_method+ will only exist on this
32- string instance. Other strings will not have +my_method+. You may also
33- override existing methods on just one object this way.
85+ String.hello # returns "Hello, world!"
86+
87+ However, this is simply a special case of a greater syntactical power in Ruby,
88+ the ability to add methods to any object. Classes are objects, so adding
89+ class methods is simply adding methods to the Class object.
90+
91+ The syntax for adding a method to an object is as follows:
92+
93+ greeting = "Hello"
94+
95+ def greeting.broaden
96+ self + ", world!"
97+ end
98+
99+ greeting.broaden # returns "Hello, world!"
100+
101+ _self_ is a keyword referring to the current object under consideration
102+ by the compiler, which might make the use of +self+ in defining a class
103+ method above a little clearer. Indeed, the example of adding a +hello+
104+ method to the class +String+ can be rewritten thus:
105+
106+ def String.hello
107+ "Hello, world!"
108+ end
109+
110+ A method defined like this is called a "singleton method". +broaden+ will only
111+ exist on the string instance +greeting+. Other strings will not have +broaden+.
112+
113+ == Overriding
114+
115+ When Ruby encounters the +def+ keyword, it doesn't consider it an error if the
116+ method already exists: it simply redefines it. This is called
117+ _overriding_. Rather like extending core classes, this is a potentially
118+ dangerous ability, and should be used sparingly because it can cause unexpected
119+ results. For example, consider this irb session:
120+
121+ >> "43".to_i
122+ => 43
123+ >> class String
124+ >> def to_i
125+ >> 42
126+ >> end
127+ >> end
128+ => nil
129+ >> "43".to_i
130+ => 42
131+
132+ This will effectively sabotage any code which makes use of the method
133+ <code>String#to_i</code> to parse numbers from strings.
34134
35135== Arguments
36136
@@ -42,8 +142,8 @@ A method may accept arguments. The argument list follows the method name:
42142
43143When called, the user of the +add_one+ method must provide an argument. The
44144argument is a local variable in the method body. The method will then add one
45- to this argument and return the value. If given <tt>1</tt> this method will
46- return <tt>2</tt> .
145+ to this argument and return the value. If given +1+ this method will
146+ return +2+ .
47147
48148The parentheses around the arguments are optional:
49149
0 commit comments